Possible Duplicate:
How do I rename a bunch of files in the Command Prompt?

I'm coming back to Windows after several years in Linux (I'm a trucker, and the company I just leased my truck to, has features on their load board site that only work in IE, and only with compatibility mode turned on).

I also listen to a lot of audio books. I rip them from CD and download them from Audible, so I get a nice long list of MP3 files. Most of them have names like "Chapter 3 - Into the Void". But my truck stereo, which will play MP3 files on a USB flash drive, will only sort on about the first four characters of the filename. A list of 39 MP3's with filenames that all start with "Chapter" will be played back in random order. So I need to rename these files to something that starts with, say, a 2 or 3 digit number.

In Linux, I did this in a bash script to accomplish this:

for file in *.mp3
        j=$( printf "%03" "$i" )
        mv "$file" "$j.mp3"
        (( i++ ))

This would get me a directory of files named 00l.mp3, 002.mp3, 003.mp3, and so forth. But don't know how to do this in Windows.

I vaguely remember being able to do something using batch files in Windows, but the syntax is all different and the available tools to call forth are different (there is no sed or awk in Windows, unfortunately). Does anyone know where to point me to find this stuff out?


marked as duplicate by Dave, Daniel Andersson, Renan, Diogo, BBlake Jan 14 '13 at 13:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Thanks, I had seen a few similar things (on this site), but that one appears to add the required leading zeroes. So my truck's stereo won't play 10.mp3 before 3.mp3 for example :-) – Jeff Lanthripp Jan 14 '13 at 10:48
  • Yeah, I know that issue oh too well. FYI, I have an FM transmitter which plays via SD card, but, the firmware isn't too hot and it still doesn't force it to play in order! I had to actually copy the files in the correct order as it didn't playback in alphabetic order, but in creation date! – Dave Jan 14 '13 at 10:52
  • 1
    This is not a duplicate of the specified question. That is a general bulk-renaming question, and some of the answers involve GUI apps while this is specifically a question about batch-files. Moreover, this question specifically asks about padding the numbers in the filenames with leading-zeros which none of the answers on that page do. If this is a duplicate of anything, it’s of a couple of questions on SO, not on SU. – Synetech Apr 25 '14 at 6:12

Your answer exists within the body of How do I rename a bunch of files in the Command Prompt?

Although that is for command prompt, there are examples of batch scripts, which I have included below

@echo off
setlocal EnableDelayedExpansion
set i=0
for %%a in (*.jpg) do (
    set /a i+=1
    ren %%a !i!.new
ren *.new *.jpg

Another example

@echo off
FOR /f "tokens=*" %%G IN ('dir /b *.jpg') DO (call :renum "%%G")
GOTO :eof

 ren %1 %PREFIX%_%count%.jpg
 set /a count+=1
 GOTO :eof
  • Neither of those solutions (nor any on the referenced page) give leading zeros which this question specifically requires. – Synetech Apr 25 '14 at 6:10
  • @Synetech, I'm a little lost, are you saying that you can't add leading 0's when renaming files? – Dave Apr 25 '14 at 7:20
  • 2
    Yes, exactly. This question is about automatically padding the numbers as necessary in the filenames with leading zeros. The answers you referenced do not do that, neither do your sample scripts. You missed the point to the question entirely. – Synetech May 11 '14 at 2:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.