2

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:

  • mypicture_3435.jpg (file date = 01 Jan 2015 10:00)
  • mypicture_3465.jpg (file date = 04 Jan 2015 12:00)
  • myfriendspicture_2221.jpg (file date = 03 Jan 2015 12:00)
  • myfriendspicture_2226.jpg (file date = 04 Jan 2015 11:00)

I would like to get the following output:

  • ourvacation_01.jpg [mypicture_3435.jpg (file date = 01 Jan 2015 10:00)]
  • ourvacation_02.jpg [myfriendspicture_2221.jpg (file date = 03 Jan 2015 12:00)]
  • ourvacation_03.jpg [myfriendspicture_2226.jpg (file date = 04 Jan 2015 11:00)]
  • ourvacation_04.jpg [mypicture_3465.jpg (file date = 04 Jan 2015 12:00)]

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:

  • They do not keep a chronological sequence.
  • If I have say 11 items, and sort them by name, the sequence runs 1, 10, 11, 2, 3, 4...

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.

2
  • 1
    you want to use the date to the file name or just iterated numbers?
    – npocmaka
    CommentedJan 26, 2015 at 12:18
  • Not an answer from me, just a search on google to rename by Date picture taken: blog.cincura.net/…
    – Tensibai
    CommentedJan 26, 2015 at 13:54

1 Answer 1

1

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" 
5
  • I'm unsure, but will the modification date (dir /od) be in phase with the 'Date picture taken' exif information ? (I would bet it won't, specifically if two dirs from 2 usb sticks are mixed within one at first).
    – Tensibai
    CommentedJan 26, 2015 at 13:53
  • @Tensibai No, it uses the last modified date as defined by the filesystem. I was going to suggest adapting this VB Script to pull the date taken from EXIF data, but the OP is simply trying to show an example of programming to his math class. He's probably more interested in simplicity than bulletproof thoroughness.
    – rojo
    CommentedJan 26, 2015 at 14:07
  • 1
    yes, I forgot it was an exercise. But the 'test case' will have to be created with caution here, as a copy to another disk (deployment of the files for students) will set a new creation date, and the students are likely to make errors and need a fresh start. Best idea coming to my head: a zip file to uncompress at destination to keep the inner creation dates.
    – Tensibai
    CommentedJan 26, 2015 at 14:12
  • 1
    @Tensibai By default, DIR shows the last modified time so there shouldn't be any problems with the dates changing on test files.
    – aphoria
    CommentedJan 26, 2015 at 14:21
  • @aphoria You're right, my bad. I did a try, I had memory the 3 properties were modified (creation/modifcation and access time), appears it does not touch the modification time but only creation and acces time.
    – Tensibai
    CommentedJan 26, 2015 at 14:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.