help adjusting batch renamer

September 3, 2010 at 05:54:26
Specs: Windows XP
I am using the renaming script below

@echo off
if "%1"=="GoTo" goto %2
%comspec% /v:on /c %0 GoTo start
goto eof
:start
echo This program will sequencially rename all files with .JPG
echo extention from the current directory and from all its
echo subdirectories. Current directory is:
echo %CD%
echo.
echo This process is not reversible.
echo.
set input=
:input
set /P input=Do you wish to continue [Y/N]?
if /I "%input%"=="Y" goto proceed
if /I "%input%"=="N" goto eof
goto input
:proceed
for /R %%D in (.) do (
set N=0
for %%F in (%%~sD\*.JPG) do (
set /A N += 1
ren %%~sF image_!N!%%~xF
)
)
:eof

But would like the new file name to be 3 digits instead of one, so right now it produces image_1 but i am looking for image_001

thanks in advance


See More: help adjusting batch renamer

Report •


#1
September 3, 2010 at 06:38:41
@echo off & setLocal EnableDELAYedeXpansion

set N=
for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
if !N! lss 10 (
set S=00!N!
) else (
set S=0!N!
)
echo.!S!
)


=====================================
Helping others achieve escape felicity

M2


Report •

#2
September 3, 2010 at 06:50:57
Can you tell me where to insert this script?
Or post the complete script?

Thanks


Report •

#3
September 3, 2010 at 07:08:06
If you can download Python, here's an alternative


import os
from collections import defaultdict
root=os.path.join("C:\\","test")
store=defaultdict(list)
for r,d,f in os.walk(root):
   for files in f:
      if files[-3:].lower() == "jpg":
         store[os.path.join(r,files)].append(os.path.join(r,files))
padlen=len(str(len(store)))
counter=1
for item,path in store.iteritems():
   p,ext=item.rsplit(".")
   fi=p.rsplit("\\",1)
   fi[-1]="Image_"+str(counter).zfill(padlen)+".jpg"
   counter+=1
   os.rename(path[0], '\\'.join(fi) ) 

save as myscript.py and on command line

c:\test> python myscript.py

You don't have to know in advance how many zeroes to pad, as the script counts the total before hand.

GNU win32 packages | Gawk


Report •

Related Solutions

#4
September 3, 2010 at 07:13:27
Your original FOR loop:

===============================
for %%F in (%%~sD\*.JPG) do (
set /A N += 1
ren %%~sF image_!N!%%~xF
)
===============================

modified:

===============================
for %%F in (%%~sD\*.JPG) do (
set /A N+=1
if !N! lss 10 (
set S=00!N!
) else (
set S=0!N!
)
ren %%~sF image_!S!%%~xF
)


=====================================
Helping others achieve escape felicity

M2


Report •

#5
September 3, 2010 at 07:57:15
Hi,
I fixed the script to
@echo off
if "%1"=="GoTo" goto %2
%comspec% /v:on /c %0 GoTo start
goto eof
:start
echo This program will sequencially rename all files with .JPG
echo extention from the current directory and from all its
echo subdirectories. Current directory is:
echo %CD%
echo.
echo This process is not reversible.
echo.
set input=
:input
set /P input=Do you wish to continue [Y/N]?
if /I "%input%"=="Y" goto proceed
if /I "%input%"=="N" goto eof
goto input
:proceed
for %%F in (%%~sD\*.JPG) do (
set /A N+=1
if !N! lss 10 (
set S=00!N!
) else (
set S=0!N!
)
ren %%~sF imagetest_!S!%%~xF
)
)
:eof

and it now complety doesnt work, it doesnt rename the files at all,
please help


Report •

#6
September 3, 2010 at 08:32:04
so i figured out whats wrong and the script is working fine now, thanks for everyones help

the script that works is below

@echo off
if "%1"=="GoTo" goto %2
%comspec% /v:on /c %0 GoTo start
goto eof
:start
echo This program will sequencially rename all files with .JPG
echo extention from the current directory and from all its
echo subdirectories. Current directory is:
echo %CD%
echo.
echo This process is not reversible.
echo.
set input=
:input
set /P input=Do you wish to continue [Y/N]?
if /I "%input%"=="Y" goto proceed
if /I "%input%"=="N" goto eof
goto input
:proceed
for /R %%D in (.) do (
set N=0
for %%F in (%%~sD\*.JPG) do (
set /A N += 1
if !N! lss 10 (
set S=00!N!
) else (
set S=0!N!
)
ren %%~sF image_!S!%%~xF
)
)
:eof


Report •

Ask Question