Computing.Net > Forums > Programming > Rename files incrementally

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Rename files incrementally

Reply to Message Icon

Name: Josip
Date: October 10, 2009 at 23:56:45 Pacific
OS: Windows XP
Subcategory: Batch
Tags: Batch file, Batch Sript, text file, XP, incremental
Comment:

I need a batch script that can rename images in a folder incrementally with 2 digits.

User input would be highly desirable as I have pictures taken from holidays and want to label them accordingly.

Example:
DSC_3456.JPG to France_Paris_01.jpg
DSC_3457.JPG to France_Paris_02.jpg
DSC_3458.JPG to France_Paris_03.jpg
DSC_3459.JPG to France_Paris_04.jpg

I have checked everywhere but couldn’t find something that does what I’m asking with 2 or 3 digits and with user input.

Any help would be greatly appreciated.

Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: nbrane
Date: October 11, 2009 at 14:43:35 Pacific
Reply:

Hello: I crashed my only XP system, so I can't study the
XP batch commands which look to have far more extensive
capabilities than winME. what you want to do might be
batchable: dir /b %1*.jpg could create a bare-filename list as the basis of a batchfile, and "find /n /v" can put numbers onto listing, but the'yre on the front of the lines instead of the ends.
edlin will accept a pipe input but won't handle cut/paste, and
edit will cut/paste but won't run non-interactively (pipe).
well, sorry about the lengthy analysis, but the point of all this
is i think you need to use a basic program. the following will
build a renaming batchfile:
EXT$ = ".JPG"
C = 0
Q$=CHR$(34)
' T tells how may zeros to pad the number with
' the value 2 will look like: 01,02 3 looks like 001,002
T = 2
INPUT "FROM NAME: ", N$
INPUT "TO NAME: ", L$
SHELL "DIR /B " + N$ + "*" + EXT$ + " > DLIST"
OPEN "DLIST" FOR INPUT AS #1
OPEN "CHANGE.BAT" FOR OUTPUT AS #2
L$=Q$+L$+"-"
DO WHILE NOT EOF(1)
LINE INPUT #1, K$
C = C + 1
C$ = LTRIM$(STR$(C))
C$ = STRING$(T - LEN(C$), "0") + C$
K$=Q$+K$+Q$
C$=L$+C$+Q$
PRINT #2, "REN " + K$ + " " + C$
LOOP
CLOSE
'OPTIONAL:
'SHELL "CHANGE.BAT"
'END
this is just a quick hack, but it would work.
the batch-file is commented out so that you can examine it before running it manually. just remove the ' from the last
couple lines to have the program do it.


0

Response Number 2
Name: dtech10
Date: October 11, 2009 at 15:39:34 Pacific
Reply:

Hi Josip

@echo off
SetLocal EnableDelayedExpansion

set Num=0
for /f %%a in ('dir /b *.jpg') do (
set /a Num+=1
if !Num! LSS 10 (ren %%a France_Paris_0!Num!.jpg
) else (ren %%a France_Paris_!Num!.jpg)
)


1

Response Number 3
Name: Josip
Date: October 12, 2009 at 17:10:02 Pacific
Reply:

nbrane,

I did as you stated, by removing the ' from the last couple of lines and the script doesn't work.

Is it a fully compliant XP DOS batch file?
--------------------------------------------------------------------------

dtech10,

You solution does work but I have some questions.
1: How do I change it to 3 digits if I need to for large number of pictures?
2: I noticed *.jpg doesn't work on pictures with brackets (1), any way of fixing that?
3: How do I get user input working to rename the files accordingly?

Thanks for your help guys.


0

Response Number 4
Name: Mechanix2Go
Date: October 12, 2009 at 18:36:55 Pacific
Reply:

I can do the user input and the 3 digits. No idea about brackets.

====================================
@echo off & setLocal EnableDELAYedExpansion

set /p new=new name ? :
set N=0
for /f %%a in ('dir /b *.jpg') do (
set /a N+=1
if !N! LSS 100 set S=0!N!
if !N! LSS 10 set S=00!N!
echo ren %%a "!new!!S!.jpg"
)


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

M2


0

Response Number 5
Name: Josip
Date: October 13, 2009 at 01:03:15 Pacific
Reply:

Hi Mechanix2Go,

Thanks for your help, but it doesn't work properly.

The max digit it will go up to is 099 beyond that it fails with the below error message.

Error Msg: "A duplicate file name exists, or the file cannot be found."

It looks like it trying to start the number sequence from the start "001" which there is already a file already renamed to that.


0

Related Posts

See More



Response Number 6
Name: nbrane
Date: October 13, 2009 at 01:44:21 Pacific
Reply:

hi again: mech, dtech & josip are your avenues to succes, not
my kludge. suggest putting output from batch into a
text file and viewing contents to see where the glitch
is coming from. basic debuggiin tech., look at output at
various points into the procedure to see what data looks
like. also consider longfilenames need quotes possiblitity.
luck


0

Response Number 7
Name: Mechanix2Go
Date: October 13, 2009 at 01:49:30 Pacific
Reply:

@echo off & setLocal EnableDELAYedExpansion

set /p new=new name ? :
set N=0
for /f %%a in ('dir /b *.jpg') do (
set /a N+=1
if !N! LSS 1000 set S=0!N!
if !N! LSS 100 set S=00!N!
if !N! LSS 10 set S=000!N!
echo ren %%a "!new!!S!.jpg"
)


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

M2


0

Response Number 8
Name: Josip
Date: October 13, 2009 at 07:02:17 Pacific
Reply:

Mechanix2Go,

Well it does rename more than 100 files now but the problem is that its adding extra zeros where not needed.
Files 1 to 9 are 3 digits long 001,002 etc. Files from 10 and above have 4 digits 0010.
Example:
test001.jpg
test002.jpg
test0010.jpg
test0011.jpg

--------------------------------------------------------------------------
nbrane,

Your comment wasn't helpful at all. I've debugged enough to know it’s not working. I can't do more than that as I don't know enough to write these scripts. If I could I wouldn't be here asking for help.

Thanks


0

Response Number 9
Name: dtech10
Date: October 13, 2009 at 07:12:59 Pacific
Reply:

HI Josip

I modified Mechanic2Go code, no insult intended.
just an oversight as I know your an excellent coder.

@echo off & setLocal EnableDELAYedExpansion

set /p new=new name ? :
set N=0
for /f %%a in ('dir /b *.jpg') do (
set /a N+=1
if !N! LSS 1000 set S=0!N!
if !N! LSS 100 set S=0!N!
if !N! LSS 10 set S=0!N!
echo ren %%a "!new!!S!.jpg"
)


0

Response Number 10
Name: Mechanix2Go
Date: October 13, 2009 at 07:26:55 Pacific
Reply:

Hi dtech10,

=====================
@echo off & setLocal EnableDELAYedExpansion

set /p new=new name ? :
set N=0
for /f %%a in ('dir /b *.jpg') do (
set /a N+=1
if !N! LSS 1000 set S=!N!
if !N! LSS 100 set S=0!N!
if !N! LSS 10 set S=00!N!
echo ren %%a "!new!!S!.jpg"
)


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

M2


0

Response Number 11
Name: Josip
Date: October 14, 2009 at 09:48:38 Pacific
Reply:

It works now but I have noticed that it will not rename files that have spaces in the. How do I get that to work?

Thanks dtech10 &Mechanix2Go for your help.


0

Response Number 12
Name: dtech10
Date: October 14, 2009 at 12:31:17 Pacific
Reply:

HI Josip
Put quotes around %a

ren "%%a" "!new!!S!.jpg"


0

Response Number 13
Name: Josip
Date: October 14, 2009 at 21:07:13 Pacific
Reply:

Hi dtech10,

I have already tried putting quotes and it didn't work.


0

Response Number 14
Name: Josip
Date: October 15, 2009 at 00:03:31 Pacific
Reply:

I figured it out.

You do need the quotes around the "%%a" but there was another thing that I added that made it work.

I added "tokens=* delims=" into:
for /f %%a in ('dir /b *.jpg') do (

Ending up with:
for /f "tokens=* delims=" %%a in ('dir /b *.jpg') do (

I have done some basic testing but it seems to work atm.


0

Response Number 15
Name: Mechanix2Go
Date: October 15, 2009 at 03:14:18 Pacific
Reply:

You don't need delims but you do need tokens.

I never think about spaces in file names because I never use them. LOL


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

M2


0

Response Number 16
Name: Josip
Date: October 15, 2009 at 07:27:08 Pacific
Reply:

Thank you Mechanix2Go & dtech10 for helping me out.

Ignore the layout as it’s a direct paste from the .bat file and doesn't show the correct formatting in this forum.

Below is my completed script that renames images inside a folder. It work by right clicking on a folder in Windows Explorer and selecting "Bulk Rename"

--------------------------------------------------------------------------
** Registry entry to enable right click on folders to rename

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\(JPG) Bulk Rename]
@="(JPG) Bulk Rename"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\(JPG) Bulk Rename\command]
@="C:\\Program Files\\Tools\\WinTools\\Scripts\\ImgProcess\\BulkRename-main.bat \"%l\""
--------------------------------------------------------------------------

** Completed Working Script

@ECHO OFF
COLOR 0A
SetLocal EnableDelayedExpansion

:RESTART
CLS
IF EXIST "%1\*.jpg" GOTO START
IF EXIST "%1\*.jpe" GOTO START
IF EXIST "%1\*.jpeg" GOTO START
ECHO.
ECHO No (JPG / JPE / JPEG) images Found
ping localhost -n 4 >NUL
GOTO END

:START
ECHO ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
ECHO º º
ECHO º (JPG) Bulk Rename º
ECHO º ****************************** º
ECHO º Updated on: 16/10/2009 º
ECHO º º
ECHO º Do not use any spaces. Seperate words with a "-" if needed º
ECHO º º
ECHO º Example #1 of Result: Australia_Melbourne_2009-08.jpg º
ECHO º Example #2 of Result: Lily_1st-Birthday_2009-04.jpg º
ECHO º º
ECHO º Type R to Restart or Q to Quit and press Enter at any time º
ECHO º º
ECHO ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
ECHO.
ECHO.

IF EXIST "%1\*.JPG" RENAME *.JPG *.jpg
IF EXIST "%1\*.JPEG" RENAME *.JPEG *.jpg
IF EXIST "%1\*.JPE" RENAME *.JPE *.jpg

ECHO Type one of the following (Place / Location / Name / Country):
ECHO Example: Donald-Trump / Czech-Republic
ECHO.
set /p Location=
if /I '%Location%'=='R' GOTO RESTART
if /I '%Location%'=='Q' GOTO END
ECHO.
ECHO ------------------------------------------------------

ECHO.
ECHO Type one of the following (Event / Town):
ECHO Example: 1st-Birthday / Dubrovnik
ECHO.
set /p Event=
if /I '%Event%'=='R' GOTO RESTART
if /I '%Event%'=='Q' GOTO END
ECHO.
ECHO ------------------------------------------------------

ECHO.
ECHO Type the Date(Year-Month):
ECHO Example: 2009-12
ECHO.
set /p Date=
if /I '%Date%'=='R' GOTO RESTART
if /I '%Date%'=='Q' GOTO END
ECHO.
CLS

:RESULTS
ECHO ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ
ECHO.
ECHO Result: %Location%_%Event%_%Date%_01.jpg
ECHO %Location%_%Event%_%Date%_02.jpg
ECHO %Location%_%Event%_%Date%_03.jpg
ECHO.
ECHO ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ
ECHO.
ECHO 1. Continue
ECHO 2. Restart
ECHO 3. Quit
ECHO.
set choice=
set /p choice=Enter option 1, 2, or 3:
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' GOTO CONTINUE
if '%choice%'=='2' GOTO RESTART
if '%choice%'=='3' GOTO END
ECHO.
ECHO.
ECHO "%choice%" is not a valid option - try again
ECHO.
ping localhost -n 3 >NUL
CLS
GOTO RESULTS

:CONTINUE
if not exist "C:\Windows\System32\find.exe" goto 2DIGIT
set ext=.jpg
for /F %%a in ('dir/b/a-d-h ^| find /c "%ext%"') do set count=%%a
if %count% LEQ 99 GOTO 2DIGIT
if %count% GTR 99 GOTO 3DIGIT

:2DIGIT
set N=0
for /f "tokens=* %%a in ('dir /b *.jpg') do (
set /a N+=1
if !N! LSS 100 set S=!N!
if !N! LSS 10 set S=0!N!
rename "%%a" %location%_%event%_%date%_!S!.jpg
)
set N=0
for /f "tokens=* %%a in ('dir /b *.NEF') do (
set /a N+=1
if !N! LSS 100 set S=!N!
if !N! LSS 10 set S=0!N!
rename "%%a" %location%_%event%_%date%_!S!.NEF
)
GOTO COMPLETED

:3DIGIT
set N=0
for /f "tokens=* %%a in ('dir /b *.jpg') do (
set /a N+=1
if !N! LSS 1000 set S=!N!
if !N! LSS 100 set S=0!N!
if !N! LSS 10 set S=00!N!
rename "%%a" %location%_%event%_%date%_!S!.jpg
)
set N=0
for /f "tokens=* %%a in ('dir /b *.NEF') do (
set /a N+=1
if !N! LSS 1000 set S=!N!
if !N! LSS 100 set S=0!N!
if !N! LSS 10 set S=00!N!
rename "%%a" %location%_%event%_%date%_!S!.NEF
)
GOTO COMPLETED

:COMPLETED
ECHO.
ECHO.
ECHO ** Completed **
ECHO ---------------
ping localhost -n 4 >NUL
GOTO END

:END
--------------------------------------------------------------------------

There is one issue that I don't know how to fix. The script will bail out if the user input enters a "space" anywhere.
How do I fix it so that it will allow spaces in the user input and therefore the script wont bail out?

Can you please check my script and see if there is anything wrong with it. Any refinements and fixes to the script would be greatly appreciated.

Thanks


0

Response Number 17
Name: Mechanix2Go
Date: October 15, 2009 at 08:18:47 Pacific
Reply:

I lost interest after about 50 lines but it's usually a bad idea to reset built-in vars, like DATE.


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

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Rename files incrementally

Renaming files www.computing.net/answers/programming/renaming-files/11132.html

Renaming files using a batch www.computing.net/answers/programming/renaming-files-using-a-batch/11819.html

Renaming files www.computing.net/answers/programming/renaming-files/11768.html