Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 3 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

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:


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

A complete solution is below.

Set the variables in section "variables":

@echo off

::only to tell user what this bat are doing

::enable that thing to allow, for example, incremental counter in a for loop :)
echo.- EnableDelayedExpansion
SETLOCAL EnableDelayedExpansion

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
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
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!"

::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
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .