Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to batch-rename files containing numbers, padding the numbers with zeroes. The goal is to sort numbered sets of files alphabetically in the correct order in any application.

In a set of files with the same prefix text followed by a number, I want to pad each number with as many zeroes as the highest number has digits.

I.e a list of files named like this:

xx 1 abc.txt
xx 2 def.txt
xx 3 ghi.txt
xx 3.5 jkl.txt
xx 4a mno.txt
xx 4b pqr.txt
xx 100 end.txt

should become:

xx 001 abc.txt
xx 002 def.txt
xx 003 ghi.txt
xx 003.5 jkl.txt
xx 004a mno.txt
xx 004b pqr.txt
xx 100 end.txt

(System is Windows XP)

(Windows Explorer uses some kind of heuristic to sort files with numbers in this way, not always correctly. I am however looking for a way to actually rename the files)


I was refered to this question as a possible duplicate: How can I mass rename files from the command line or using a 3rd party tool? The tools mentioned there (Bulk Rename Utility, AntRenamer, Free Commander Portable etc.) are as far as I can tell not capable of detecting the minimum amount of paddding needed for each set of files.

My thinking was this: Explorer is capable of detecting files with consecutive numbers, even if they are left-aligned without padding, as in my first example above. It uses this detection in order to sort the files correctly.
(see for example )
I am looking for a tool that does the same detection, but also renames the files accordingly.
(Finding the largest number for each related set of files, not simple the largest number in the whole directory, and then padding the numbers in each set.)

Or to put it annother way: I want to do a "natural sort" for all files in a directory, then rename them so that the ascii sort is identical to the "natural sort"

I now suspect that there is no tool capable of doing this, so I probably just need to write a script.

share|improve this question
You can always write a program or a script for this. I'd personally use Python. – Doc Jul 13 '11 at 22:24
This can be easily achieved if you're using *nix. For Windows, you'll have to install an environment to run some scripts, unless some bat guru sees this page... – Jul 25 '11 at 2:56
Powershell. Sounds too complicated for a bulk renamer. – viking Jul 25 '11 at 3:09
Why wouldn't a batch renamer (GUI, not script) work? If I understand correctly, you just need to insert two zeroes for numbers less than 10, or one zero for numbers less than 100. Two steps may not be as easy as one, but it still seems pretty darn easy to accomplish. – Nicholas Cole May 24 '13 at 19:19

This should work at its basic level. Make a backup first!

cd to dir containing files: 99999 - description.txt


opendir(DIR, ".");
@files = sort(grep(/txt$/i, readdir(DIR)));

foreach $file (@files) {
    print $file."\n";
    if ($file=~/xx (\d+)(.*\.txt)$/i) {
foreach $file (@files) {
    if ($file=~/xx (\d+)(.*\.txt)$/i) {
        $newfile=sprintf("%0${len}d%s", $1, $2);
        rename("$file", "$newfile");
share|improve this answer
I saw that the criteria was different here than in the other thread. Just play with the regexps until it works. – Andy Lee Robinson Jul 25 '11 at 3:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .