Question
June 20, 2007 at 09:08 AM
wirtzgirl

Renaming JPG Files With a Batch File

by wirtzgirl . Updated 19 years, 4 months ago

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 discussion is locked

All Comments