Hi I'd like to create a bat file to rename files using the cmd. Say a friend and I went on vacation together. We have put all our pictures together and I now want to rename all the pictures in a sequence.
Say I selected the following pictures:
I would like to get the following output:
This is easy to do with specific software such as acdsee or even with Windows' image browser. But I would like to do it using the command promt. (I teach mathematics in a school and I would like to use this as a programming example).
I tried the follwing script and it worked:
@echo off
setlocal EnableDelayedExpansion
set i=0
for %%a in (*.jpg) do (
set /a i+=1
ren "%%a" "!i!.new"
)
ren *.new *.jpg
But it renamed the files like this:
myfriendspicture_2221.jpg
becomes 1.jpg
myfriendspicture_2226.jpg
becomes 2.jpg
mypicture_3435.jpg
becomes 3.jpg
mypicture_3465.jpg
becomes 4.jpg
So the problems are:
I tried to run dir /od before I rename, but it didn't work, the sequence runs according to the file name.
All the strings I have found regarding this issue append the current date and time to the file name, but not the file date and time to it's own name.
I couldn't find a way to add a leading 0 or 00 to the sequence so that the file name and chronological order match.
Thank you very much in advance for the help.
I tried to run dir /od before I rename, but it didn't work, the sequence runs according to the file name.
Did you simply run dir /od
and then for %%a in (*.jpg)
? How did you plan to capture the output of dir /od
to make it useful, rather than just dumping it to stdout?
Whenever you want to capture the output of a command, use a for /f
loop. You should combine the two commands like this:
for /f "delims=" %%I in ('dir /b /o:d *.jpg') do stuff.
Next issue: zero left padding the file names. Can we assume you won't have more than 10,000 pics from your vacation? Then just prepend four zeros to your generated filename, then use the right-most 4 characters to determine the base name of your pics.
@echo off
setlocal enabledelayedexpansion
set /a seq=10001
for /f "delims=" %%I in ('dir /b /o:d *.jpg') do (
rem :: remove the "echo" when you are ready to rename
echo ren "%%~I" "ourvacation_!seq:~-4!.jpg"
set /a seq += 1
)
The result will look a little something like this:
C:\Users\me\Desktop>test.bat
ren "avatar65929_2.jpg" "ourvacation_0001.jpg"
ren "IMG_20140621_190332.jpg" "ourvacation_0002.jpg"
ren "funny-Finding-Neverland-scene-Explorer.jpg" "ourvacation_0003.jpg"
ren "5DFPwMa.jpg" "ourvacation_0004.jpg"
ren "funny-root-math-equation.jpg" "ourvacation_0005.jpg"
ren "196889_145889312230730_1041953273_n.jpg" "ourvacation_0006.jpg"
ren "0001-cf72d77a-509ac348-b92c-fe8af7d2.jpg" "ourvacation_0007.jpg"
ren "IMG_20141230_191526.jpg" "ourvacation_0008.jpg"
ren "20150120_142150.jpg" "ourvacation_0009.jpg"
ren "20150120_145223.jpg" "ourvacation_0010.jpg"