Computing.Net > Forums > Programming > Batch Rename files in a folder

Batch Rename files in a folder

Reply to Message Icon

Original Message
Name: pball
Date: February 18, 2007 at 13:29:53 Pacific
Subject: Batch Rename files in a folder
OS: xp home
CPU/Ram: 2.0 ghz 512 mb
Model/Manufacturer: home made
Comment:

I have a large amount of folders with many files in each one and I want to to be able to rename all of the files in the sub folders to the name of the folder with numbers in the form 001 002 etc.

so the files in the folder Hey would be renamed to Hey_001 with the original extension

Also I continuously add files to these folders, so is it possible to find what the last correctly numbered file is and continue with the numbers from there? so there aren't any errors or every file getting rename since the new files are alphabetically in front of the older files.

one last thing could you point me to a good batch file guide or tutorial, I know a good bit but nothing about variables and the more complex parts of batch scripting
thanks


Report Offensive Message For Removal


Response Number 1
Name: dtech10
Date: February 22, 2007 at 06:36:14 Pacific
Reply: (edit)

Hi pball
How many leading zeros do you want ie
001 (2 off) and do you want the leading underline before the numbers ater the directory name.



Report Offensive Follow Up For Removal

Response Number 2
Name: pball
Date: February 22, 2007 at 15:17:25 Pacific
Reply: (edit)

yes i want to have it like
folder_001.ext folder_002.ext folder_003.ext
with the extension the same as before
if the folder is made of two or more words with spaces I don't mind if the spaces are kept in the file name or if it'd be easier to remove them


Report Offensive Follow Up For Removal

Response Number 3
Name: dtech10
Date: February 22, 2007 at 16:36:12 Pacific
Reply: (edit)

Hi pball

The first time you run the file it
will first come up with "File Not Found"
This is due to Findstr not finding any
files beginning with the directory name.
I,ll work on that.
After some files have been renumbered
everything should be ok.
Run the Batch file above the directories
you want to process.
I ran it ok in WinXP Pro, so I shouldnt
think you will have any problems.
Famous last words.


@echo off
SetLocal EnableDelayedExpansion
cls
for /d /r %%a in (*.*) do (
call :LastDir %%a
call :NoFiles %%a
call :ReName %%a
)
exit /b

:Zeros %1 %2
set y=%1
set Z=000000
if %2 GEQ 10 (set /a y-=1) & if %2 GEQ 100 (set /a y-=1)
if %2 GEQ 1000 (set /a y-=1) & if %2 GEQ 10000 (set /a y-=1)
set Num=!Z:~0,%y%!%2
exit /b

:LastDir %*
set Arg=%*\
for /l %%c in (1,1,50) do (
set C=!Arg:~%%c,1!
set D=!D!!C!
if !C!==\ (set E=!D! & set D=)
)
set Dirs=!E:~,-2!
exit /b


:NoFiles %1
set Count=0
for /f %%a in ('dir /b %1 ^| findstr /b "!Dirs!_[0-9][0-9][0-9]"') do set /a Count+=1
exit /b


:ReName %1
for /f "tokens=*" %%a in ('dir /b /a-d %1 ^| findstr /v !One!_[0-9][0-9][0-9]') do (
set /a Count+=1
call :Zeros 2 !Count!
ren "%1\%%a" !Dirs!_!Num!%%~xa
)
exit /b


Report Offensive Follow Up For Removal

Response Number 4
Name: dtech10
Date: February 23, 2007 at 09:16:34 Pacific
Reply: (edit)

Hi pball
Bugs solved.
The previous didnt handle directories
with spaces in them.
Not File Not found fixed.
Run from the directory above the
directories you want to process.
You may need to incease the the
count in this line if your
directory path is longer than
50 chars.
ie :LastDir subroutine
"for /l %%c in (1,1,50) do ("
I've put in the main loop the
command "echo [!Dirs!]"
so you can see if 50 is big enough
to display your directory path.
del the "echo [!Dirs!]" when
happpy.


-----
@echo off
SetLocal EnableDelayedExpansion
cls
for /d /r %%a in (*.*) do (
call :LastDir %%a
echo [!Dirs!]
call :NoFiles %%a
call :ReName %%a
)
exit /b

:Zeros %1 %2
set y=%1
set Z=000000
if %2 GEQ 10 (set /a y-=1) & if %2 GEQ 100 (set /a y-=1)
if %2 GEQ 1000 (set /a y-=1) & if %2 GEQ 10000 (set /a y-=1)
set Num=!Z:~0,%y%!%2
exit /b

:LastDir %*
set Arg=%*\
for /l %%c in (1,1,50) do (
set C=!Arg:~%%c,1!
set D=!D!!C!
if !C!==\ (set E=!D! & set D=)
)
set Dirs=!E:~,-2!
exit /b


:NoFiles %*
set Count=0
for /f %%a in ('dir /b "%*" ^| findstr /b "!Dirs!_[0-9][0-9][0-9]"') do set /a
Count+=1
exit /b


:ReName %*
set Found=
for %%a in ("%*\*.*") do set Found=Yes
if not "!Found!" EQU "Yes" exit /b
for /f "tokens=*" %%a in ('dir /b /a-d "%*" ^| findstr /v "!Dirs!_[0-9][0-9][0-9]"') do (
set /a Count+=1
call :Zeros 2 !Count!
ren "%*\%%a" "!Dirs!_!Num!%%~xa"
)
exit /b



Report Offensive Follow Up For Removal

Response Number 5
Name: pball
Date: February 24, 2007 at 20:02:23 Pacific
Reply: (edit)

thanks for the help
but I've notice it does a couple of funny things
It didn't rename one png file then I changed that file's name to something random then it named it as the 001 file when there already was a 001
It works ok other than that, but I think I'm just too picky and i what I want to do may not be easily possible with batch
Thanks again


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch Rename files in a folder

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge