Question

Locked

Renaming JPG Files With a Batch File

By wirtzgirl ·
I'm a bit green when it comes to writing batch files, so there may be a simple answer to this problem. I would like to use a batch file to rename several JPG files with a name that includes the timestamp and the last four characters of the old filename. Here's the current version of the code:

@ECHO OFF
FOR %%V IN (D:\test\*.jpg) DO (
FOR /F "tokens=1-5 delims=/: " %%J IN ("%%~tV") DO (
SET newfile=%%V
SET newfile=GR%%L%%J%%K%%M%%N-%newfile:~-8%
ECHO Current filename is "%%V"
ECHO New filename will be "%newfile%"
IF EXIST %newfile% (
ECHO Cannot rename %%V
) ELSE (
ECHO Renaming "%%V" to "%newfile%"
RENAME "%%V" %newfile%
)
SET newfile=
)
)

The only output I get from running this program is "The syntax of the command is incorrect." From the debugging I've done, it appears that the variable %newfile% is not getting populated with a value. I can get the rest of the code to work if I ignore the current name of the file like this:

@ECHO OFF
FOR %%V IN (D:\test\*.jpg) DO (
FOR /F "tokens=1-5 delims=/: " %%J IN ("%%~tV") DO (
ECHO Current filename is "%%V"
ECHO New filename will be "GR%%L%%J%%K%%M%%N%%~xV"
IF EXIST GR%%L%%J%%K%%M%%N%%~xV (
ECHO Cannot rename %%V
) ELSE (
ECHO Renaming "%%V" to "GR%%L%%J%%K%%M%%N%%~xV"
RENAME "%%V" GR%%L%%J%%K%%M%%N%%~xV
)
)
)

But I would really, really like to include the last four letters of the current filename. Any ideas?

I'm running Win XP.

This conversation is currently closed to new comments.

6 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

set newfile= ? twice

by 1bn0 In reply to Renaming JPG Files With a ...

From a cursory glance at your batch file:
You set newfile=%%V

Then you are immediately overwirting that setting with a different string.

I seem to recall that won't work.

try using a different tactic.
SET newfile=%%v
SET newfile1=GR%%L%%J%%K%%M%%N-%newfile:~-8%
set newfile=%newfile1


I'm sure my syntax is wring but I hope you get the idea.

Create variable1
create variable2 based on variable1
set variable = variable2

continue processing with new value for variable1

Collapse -

Attempting to use second variable

by wirtzgirl In reply to set newfile= ? twice

Using a second variable isn't working, either. Nice try, though. I'm pretty sure it's a SET problem or some kind of newbie error.

Collapse -

Set not working in loop

by gibsonrd In reply to Renaming JPG Files With a ...

When I call the batch from a command prompt, it worked. But executing it from the system, the set did not work within the loop. I changed it to use 2 batch files, #1 executing the loop and #2 doing the work, as follows:
renamePix1.bat
FOR %%V IN (D:\testing\*.jpg) DO renamePix2 %%V

renamePix2.bat
@echo off
SET curfile=%1
SET Yr=%date:~10%
SET Mn=%date:~4,2%
SET Dy=%date:~7,2%
SET Hr=%time:~0,2%
SET Mnt=%time:~3,2%
ECHO Current filename is %curfile%
SET newfile=GR%Yr%%Mn%%Dy%-%Hr%%Mnt%-%curfile:~-8%
ECHO New filename will be %newfile%

IF EXIST %newfile% (
ECHO Cannot rename %1
pause
) ELSE (
ECHO Renaming "%1" to "%newfile%"
RENAME %1 %newfile%
)

Collapse -

The problem is using SET in a FOR loop

by HavaCigar In reply to Renaming JPG Files With a ...

The SET commands won't take effect until the FOR loop is exited.

You can get around this with a SETLOCAL command. And, since you have nested loops, you need two SETLOCAL commands. When you use SETLOCAL, the references to environment variables need to change from % to !.

Try the following. I left in multiple variables for the filenames for clarity:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%V IN (D:\test\*.jpg) DO (
FOR /F "tokens=1-5 delims=/: " %%J IN ("%%~tV") DO (
SETLOCAL ENABLEDELAYEDEXPANSION
SET oldfile=%%V
SET newfile1=GR%%L%%J%%K%%M%%N-
SET newfile2=!oldfile:~-8!
SET newfile=!newfile1!!newfile2!
ECHO Current filename is "!oldfile!"
ECHO New filename will be "!newfile!"
IF EXIST !newfile! (
ECHO Cannot rename !oldfile!
) ELSE (
ECHO Renaming "!oldfile!" to "!newfile!"
RENAME !oldfile! !newfile!
)
SET newfile=
SET newfile1=
SET newfile2=
SET oldfile=
)
ENDLOCAL
)
ENDLOCAL

If you want, you can use only the first SETLOCAL command and end your FOR /F loop sooner, thus getting rid of one SETLOCAL
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%V IN (D:\test\*.jpg) DO (
SET oldfile=%%V
FOR /F "tokens=1-5 delims=/: " %%J IN ("%%~tV") DO (
SET newfile1=GR%%L%%J%%K%%M%%N-
)
SET newfile2=!oldfile:~-8!
SET newfile=!newfile1!!newfile2!
ECHO Current filename is "!oldfile!"
ECHO New filename will be "!newfile!"
IF EXIST !newfile! (
ECHO Cannot rename !oldfile!
) ELSE (
ECHO Renaming "!oldfile!" to "!newfile!"
RENAME !oldfile! !newfile!
)
SET newfile=
SET newfile1=
SET newfile2=
SET oldfile=
)
ENDLOCAL


Or, if you don't like the extra variables:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%V IN (D:\test\*.jpg) DO (
SET newfile=%%V
FOR /F "tokens=1-5 delims=/: " %%J IN ("%%~tV") DO (
SET newfile=GR%%L%%J%%K%%M%%N-!newfile:~-8!
)
ECHO Current filename is "%%V"
ECHO New filename will be "!newfile!"
IF EXIST !newfile! (
ECHO Cannot rename %%V
) ELSE (
ECHO Renaming "%%V" to "!newfile!"
RENAME %%V !newfile!
)
SET newfile=
)
ENDLOCAL


I edited it to change the reference back to D:, I was using C:.

Collapse -

download.com

by dbell In reply to Renaming JPG Files With a ...

Check out "A.F.5 Rename your files 1.1" at download.com. This may be able to do what you want.

Back to Software Forum
6 total posts (Page 1 of 1)  

Software Forums