Question

I wrote this batch file to append some text to the filenames of a set of jpeg files, giving the option to append them before or after the current filename.

But for some odd reason when appending before the filename, one file is being proccessed twice getting the result as new_new_FileName.jpg while all other files are getting just new_FileName.jpg.

Interesting enough, this problem isn't happening always, as well as when appending after the filename it is always working fine.

Following is the entire code, with no visual difference between the before or after, but still resulting differently. Can anyone please examine this file and explain me where I'm wrong?

Help is appreciated.

@ECHO off
title Rename Script
set /A count=1
:Start
cls
set /p STR=choose a string to append:
cls
echo 1. Append before
echo 2. Append after
set /p choice=I choose (1,2):
if %choice%==1 goto renameb
if %choice%==2 goto renamea
:renameB
cls
echo Appending '%STR%' before current file name.
echo.
set /A count=0
FOR %%a in (*.jpg) DO (
    ren "%%~a" "%STR%%%~na%%~xa"
    echo Was: %%~a Became: %STR%%%~na%%~xa
    set /A count+=1
)
goto end
:renameA
cls
echo Appending '%STR%' after current file name.
echo.
set /A count=0
FOR %%a in (*.jpg) DO (
    ren "%%~a" "%%~na%STR%%%~xa"
    echo Was: %%~a Became: %%~na%STR%%%~xa
    set /A count+=1
)
goto end
:end
echo[
echo %count% files were renamed.
echo[
echo The process in now done.
pause
Was it helpful?

Solution

for renaming, the simple for loop doesn't work:

FOR %%a in (*.jpg) DO (

for some reasons from the old CP/M days with the FCB's a for /f loop is needed:

for /f "delims=" %%a in ('dir /a-d /b *.jpg') do (
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top