I'm trying to create a batch file that I can copy and paste into any folder (I don't want to have to change the directory path every time I use it) that has multiple jpg files in it such that when I run the .bat file, it will rename all the jpg filenames in that folder with a specific title keeping the jpg files in the same order as they were originally. I want the first jpg in the folder to be renamed as 1 - Front, then the next jpg to be renamed as 2 - Back, then the next jpg to be renamed as 3 - Left Side, then the next jpg to be renamed as 4 - Right Side, then the next jpg to be renamed as 5 - Top, then the next jpg to be renamed as 6 - Bottom
I've tried 4 scripts. The first:
@echo off
setlocal enabledelayedexpansion
REM Get the current directory where the batch file is located
set "folderPath=%~dp0"
REM Initialize the counter
set "counter=1"
REM Define the desired filenames
set "filenames=Front Back Left Side Right Side Top Bottom"
REM Loop through each JPG file in the folder
for %%F in ("%folderPath%\*.jpg") do (
REM Rename the file based on the counter and filenames
for %%A in (!counter!) do (
ren "%%F" "%%A - !filenames:~%%A,1!.jpg"
)
REM Increment the counter
set /a "counter+=1"
)
This renames the first file as 1 - r, the second file as 2 - o, the third file as 3 - n, the fourth file as 4 - t, the fifth file as 5 - , and the sixth file as 6 -
I also tried this:
@echo off
setlocal enabledelayedexpansion
REM Get the current directory where the batch file is located
set "folderPath=%~dp0"
REM Initialize the counter
set "counter=1"
REM Define the desired filenames
set "filenames=Front Back Left Side Right Side Top Bottom"
REM Loop through each JPG file in the folder
for %%F in ("%folderPath%\*.jpg") do (
REM Get the corresponding filename from the list
for %%A in (!counter!) do (
for %%B in (!filenames!) do (
if %%A==1 (
ren "%%F" "%%A - %%B.jpg"
) else (
REM Increment the counter
set /a "counter+=1"
if %%A==2 (
ren "%%F" "%%A - %%B.jpg"
) else (
REM Increment the counter
set /a "counter+=1"
if %%A==3 (
ren "%%F" "%%A - %%B.jpg"
) else (
REM Increment the counter
set /a "counter+=1"
if %%A==4 (
ren "%%F" "%%A - %%B.jpg"
) else (
REM Increment the counter
set /a "counter+=1"
if %%A==5 (
ren "%%F" "%%A - %%B.jpg"
) else (
REM Increment the counter
set /a "counter+=1"
if %%A==6 (
ren "%%F" "%%A - %%B.jpg"
)
)
)
)
)
)
)
)
)
This renames the files as 1 - Back, 1 - Front, 1 - Left, 1 - Right, 1 - Side, 1 - Top.
Next, I tried:
@echo off
setlocal enabledelayedexpansion
REM Get the current directory where the batch file is located
set "folderPath=%~dp0"
REM Initialize the counter
set "counter=1"
REM Define the desired filenames
set "filenames=Front Back Left Side Right Side Top Bottom"
REM Loop through each JPG file in the folder
for %%F in ("%folderPath%\*.jpg") do (
REM Rename the file based on the counter and filenames
for %%A in (!counter!) do (
for %%B in (!filenames!) do (
if %%A==%%B (
ren "%%F" "%%A.jpg"
)
)
)
REM Increment the counter
set /a "counter+=1"
)
And finally, I tried:
@echo off
setlocal enabledelayedexpansion
REM Get the current directory where the batch file is located
set "folderPath=%~dp0"
REM Define the desired filenames
set "filenames=Front Back Left Side Right Side Top Bottom"
REM Initialize the counter
set "counter=1"
REM Loop through each JPG file in the folder
for %%F in ("%folderPath%\*.jpg") do (
REM Extract the corresponding filename from the list
for %%A in (!counter!) do (
for %%B in (!filenames!) do (
if %%A==%%B (
set "newName=%%B.jpg"
ren "%%F" "!newName!"
)
)
)
REM Increment the counter
set /a "counter+=1"
)
Neither of these last two did anything. Can someone please help? Thanks!
This is just one possible way you could do this. Output the new filenames to a temporary file. Then use that as standard input to the code block. This will get captured by the SET /P
command. Use a FOR /F
command to capture the output of the dir
command. It is essential that this is used versus a normal FOR
command. All files are enumerated by the FOR /F before it executes. With a normal FOR command it processes one file at a time which will cause issues with files entering back into the queue. I am just echoing the rename to show you what it will do. If it looks correct just remove the echo from the rename command.
@echo off
setlocal enabledelayedexpansion
REM List JPG files in directory
dir /a-d /b /on *.jpg
(
echo 1 - Front
echo 2 - Back
echo 3 - Left
echo 4 - Right Side
echo 5 - Top
echo 6 - Bottom
)>tmp.txt
<tmp.txt (FOR /F "delims=" %%G IN ('dir /a-d /b /on *.jpg') DO (
set /p newname=
echo rename "%%G" "!newname!%%~xG"
)
)
del tmp.txt
Here is the execution.
D:\My Drive\BatchFiles\SO\78206913>so.bat
bar.jpg
foo.jpg
green.jpg
pink.jpg
purple.jpg
red.jpg
rename "bar.jpg" "1 - Front.jpg"
rename "foo.jpg" "2 - Back.jpg"
rename "green.jpg" "3 - Left.jpg"
rename "pink.jpg" "4 - Right Side.jpg"
rename "purple.jpg" "5 - Top.jpg"
rename "red.jpg" "6 - Bottom.jpg"