Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
I think the error occurs for each folder that does not have "jpeg" files inside. –  kokbira Jul 12 '11 at 20:31
add comment

3 Answers

up vote 4 down vote accepted

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.

share|improve this answer
    
ok, your solution is smaller :) - I used an old one I have in my bat library. I did not tested yours but I believe on you. –  kokbira Jul 12 '11 at 20:29
add comment

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.

share|improve this answer
add comment

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
share|improve this answer
    
a "file not found" appears for each folder that does not have "jpeg" files on it. –  kokbira Jul 12 '11 at 20:31
1  
Whoa. Crazy stuff. And what is that business with %PATH%? –  paradroid Jul 12 '11 at 20:34
    
well, when I created my first version of a renamer batch, I did not know about pushd and popd, haha. so "::go to %path% and its driveletter " = pushd, ::return to %pathbak% and its driveletter = popd :) –  kokbira Jul 12 '11 at 20:50
    
but I think is is so didactic and a "more controlled" solution –  kokbira Jul 12 '11 at 20:51
    
I see. You could just use %SystemDrive% if you wanted the system drive for whatever reason. –  paradroid Jul 12 '11 at 21:02
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.