Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: pball
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

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.

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

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

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

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

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |