windows - How to loop through folders and rename extensions in a batch file?

16
2014-04
  • user839765

    Selecting a single folder I could run

    ren *.jpeg *.png
    

    I have multiple folders. I want to create a batch file that will loop through all the folders and will rename the files within it.

    For example I have folders TEST1 and TEST2 inside root directory TEST. I want to create a batch that will rename all the .jpeg files within TEST1 and TEST2 to .png.

    FOR /R "E:\TEST\" %%G in (*.jpeg) DO ren *.jpeg *.png
    

    I am getting an error:

    The system can not find the file specified

    I don't understand the issue.

  • Answers
  • paradroid

    You are not applying the for command to the ren action.

    for /r "E:\test\" %%G in (*.jpeg) do ren "%%~G" *.png
    

    You need to change %% to % if you are doing this interactively, and not in a batch file.

    The ~ strips quotes, which are re-added, to avoid any possible errors with paths which contain spaces.

  • JdeBP

    Or, if you want a slightly shorter one-liner, here's how do to it with a combination of the REN commmand and the GLOBAL commmand in JP Software's TCC/LE:

    GLOBAL REN *.JPEG *.PNG
    

    Use GLOBAL's /I option if you want to ignore any non-zero exit codes from the REN command, of course.

  • kokbira

    A complete solution is below.

    Set the variables in section "variables":

    @echo off
    
    ::only to tell user what this bat are doing
    echo.1.initializing...
    
    ::enable that thing to allow, for example, incremental counter in a for loop :)
    echo.- EnableDelayedExpansion
    SETLOCAL EnableDelayedExpansion
    
    ::variables
    echo.- variables
    :: - place here the absolute root path of your files
    set path2work="C:\Documents and Settings\ubirajara2\Desktop\Nova pasta"
    set extension2exclude=jpeg
    set extension2put=png
    
    ::only an auxiliar variable
    set pathbak=%cd%
    
    ::go to %path2work% and its drive letter
    echo.- entering the path you want
    for /f "delims=¯" %%i in ('echo.%path2work%') do %%~di
    cd %path2work%
    
    ::search all subfolders and save them to a temp file
    echo.- searching for subfolders
    echo.%path2work%>%temp%\tmpvar.txt
    for /f "delims=¯" %%i in ('dir /s /b /on /ad') do echo."%%i">>%temp%\tmpvar.txt
    
    ::execute command for root folder and all found subfolders
    echo.
    echo.2.executing...
    for /f "delims=¯" %%i in (%temp%\tmpvar.txt) do (
      cd %%i
      echo.- in folder: %%i
      for /f "delims=¯" %%j in ('dir /b /on /a-d *.%extension2exclude%') do (
        set newname=%%~nj.%extension2put%
        echo.- renaming from "%%j" to "!newname!"...
        ren "%%j" "!newname!"
      )
    )
    
    echo.
    echo.3.exiting...
    ::return to %pathbak% and its driveletter
    for /f "delims=¯" %%i in ('echo.%pathbak%') do %%~di
    cd %pathbak%
    
    @echo on
    

  • Related Question

    windows 7 - Bulk/Batch File Renamer
  • claubervs

    I would like some/any recommendation for a Batch/Bulk File Renamer software for Windows 7.

    The thing is, I want a software that can rename files in a watched folder. By this I mean that the software must keep watch inside the folder, for new files and rename any new file that enters the folder. The renaming process should use a Regular Expression pattern or something like that. I should be able to set the renaming rules in a very customizable way.

    The usability would be that, I receive/download lots of files in a folder and I want every new file that comes in the folder and matches a regular expression to be automatically renamed. But to rename the file to the new name the program must read the original filename to figure out which name it should assign to the file.

    e.g.: I receive a file named internal.process.tutorial.part1of9.avi than the software matches the words "internal", "process", "tutorial" and "partXofY". So it can know that it can rename the file to IP.Tutorial.PartXofY.by.John.Smith.avi" replacing X by 1 and Y by 9 in the example.

    I don't know if I made myself clear enough here. For me it looks like a tricky matter to handle and I don't know if anyone out there knows a software like this, and even if it exists.

    There's this program Bulk File Rename that does a lot in renaming files but there's no watch folder feature so I guess this counts as a new question here.


  • Related Answers
  • Mike Scott

    Take a look at Hygeia, and see if it does what you need.