84

Mass Renaming Directories And Files Using Total Commander


Posted by Artem Russakovskii on March 12th, 2008 in Programming, Technology

Updated: September 16th, 2012

If you're like me, you constantly move and rename files and directories. You are also an extremely productive person with evangelical ideals of making every task as efficient as it can be. In this tutorial, I will use my favorite must-have file manager called Total Commander (formerly, Windows Commander) and its brilliant Multi-Rename Tool.

You can download a shareware version of Total Commander at www.ghisler.com. I encourage you to buy it after you try it as it'll soon become an integral part of your life. I've been using it for more than 10 years now and seriously can't imagine my computer without it.

Now for some quick tasks I'd like to accomplish using the Multi-Rename Tool in under a minute each that would otherwise take me ages (also being quite boring and tedious). In my example, I'm going to use 5 files, but feel free to extend it to any number – multi is multi after all. The tool supports regular expressions (regex) to execute more advanced operations.

And the sample files are:

vac-sick-leave request form 03-10-08.xls
vac-sick-leave request form 05-30-07.xls
vac-sick-leave request form 10-16-07.xls
vac-sick-leave request form 11-22-07.xls
vac-sick-leave request form 12-19-07.xls

I fire up Total Commander, highlight the files using the gray *, right clicking and dragging on the names, or pressing Insert – the possibilities are endless, and fire up the Multi-Rename Tool from the Files menu (or just Ctrl-M). Here's what the screen looks like now:

image

Now for some tasks. I find it better to show the capabilities of this tool in examples, rather than boring descriptions. You can find a full manual in Appendix A.

Task #1. Rename all files replacing 'vac' with 'vacation'.

Fill the boxes as follows:

Search for: vac
Replace with: vacation

Notice how the New name column changes instantly as you type to show the resulting file names.

image

Now you can press Start to make the changes final. Have no fear: there's an Undo button that will undo the last rename operation in case you made an error, even if you close and reopen the Multi-Rename Tool. You can also press the little button to the left of Start, which will reload the newly renamed filenames into the tool for further manipulations.

This solution is going to work fine in this case, but it's not going to work for all possible cases. For example, if the file name were vac-sick-evacuation request form 03-10-08.xls, the resulting filename would be vacation-sick-evacationuation request form 03-10-08.xls. In order to fix this, I will use a simple regular expression:

Search for: ^vac\b
Replace with: vacation
RegEx: check

^ would match only if vac is in the beginning of the filename and \b signifies a word boundary, meaning it wouldn't match vaca or vac3, but would match vac- or vac$. You can find the information about regex from the Total Commander manual in Appendix B at the end of this article.

Similarly, to replace all spaces with dots (replacing consecutive spaces with just 1 dot), you can enter:

Search for: \s+
Replace with: .
RegEx: check

\s – the space character, followed by +, will match 1 or more consecutive spaces.

image

Task #2. Change dates from US format MM-DD-YY to YYYY-MM-DD.

Search for: (\d{2})\-(\d{2})\-(\d{2})
Replace with: 20$3-$1-$2
RegEx: check

\d will match a digit, \d{2} will only match when 2 consecutive digits are seen, the dash symbol doesn't need to be escaped with \ in this case but it's usually a good practice to do so because it has a special meaning in regex. Finally, the parentheses signify that I want to remember what I matched and put it into $1, $2, and $3 – special variables called backreferences. As you can see, I use these backreferences in the replacement string.

image

Task #3. Number the files with a 2 digit counter, starting from 00.

Rename mask: file name: [C]. [N]
Define counter (C) Start at: 0
Define counter (C) Step by: 1
Define counter (C) Digits: 2

image

In this case, I've used a special [C] variable (this is unrelated to regex) that stands for counter and customized the counter setup. You can see some other useful variables in the screenshot.

Task #4. Rename 4 letter extensions into 3 letter ones (Excel 2007 xlsx -> Excel 2003 xls).

Let's say  now that my files have a .xlsx extension that was introduced in Excel 2007. There are actually a few ways I can think of renaming them in Total Commander, the easiest being selecting all files, pressing Shift-F6 (inline rename) and then entering *.xls. BAM! All files are now .xls. The problem is that this wouldn't work if I had a mix of .docx and .xlsx files, for instance. Another solution would be to use regex, but I want to try something different. Here goes:

Extension: [E1-3]

image

This approach replaces file extensions with the extension sub-string [1-3].

There are plenty of other tasks Total Commander lets me accomplish very efficiently, like dealing with archives, files in subdirectories, checksums, copying lists of files to clipboard, comparing directories, FTPing, searching, etc, etc, etc. Maybe I'll write about them some day. So go out there, download it, and I guarantee it will change the way you think of file management forever.

 

Appendix A. Total Commander Multi-Rename Tool manual.

With this dialog box, you can rename a list of files selected in Total Commander. Instead of * and ? wildcards, this function uses placeholders in brackets []. The new names are immediately shown in the result list, but the files are not renamed until the Start! button is pressed.

Field    Description

Rename mask: file name
    With this field, you can create a definition for a new file name. The buttons below allow to insert place holders for the previous name, parts of the name, a counter, or file date/time. Place holders are always in brackets [ ], while all other letters (without brackets) will be placed in the new name without a change.
    See below for a description of all available placeholders! Use Shift+Del to remove no longer wanted entries from the list.

Extension    Definition string for extension. In principle, all placeholders can be added to either of the two definition boxes. The rename tool will create a rename string like this: Fields in name mask + "." + Fields in extension mask. The reason why the two fields are separated is to prevent the accidental removal of file extensions, which would remove the association of files with a certain program.

Search & Replace    The string entered in the field 'Search for' is replaced by the string in 'Replace with'. The text in 'search for' is NOT case sensitive! Both fields support the placeholders (wildcards) * and ?. A * stands for any number of characters, a ? for exactly one character.

This function is applied AFTER the rename mask!
    New: You can now search+replace multiple strings in one step! The strings need to be separated by the vertical line (Alt+124).
    Example: Replace Umlauts+Accents:
    Search for: ä|ö|ü|é|è|ê|à  Replace with: ae|oe|ue|e|e|e|a

^    Respect upper-/lowercase. Can be used to replace uppercase characters with other charters than lowercase.
    Example: Replace Umlauts+Accents considering upper/lowercase:

Search for: ä|ö|ü|é|è|ê|à|Ä|Ö|Ü|É|È|Ê|À  Replace with: ae|oe|ue|e|e|e|a|AE|OE|UE|E|E|E|A

RegEx    Now supports regular expressions.

Subst.    Substitution: The entire file name will be replaced by the characters entered in the "Replace" field. If this option isn't checked, only the found expression will be replaced. You can work with subexpressions, see the sample in regular expressions.

Upper/lowercase    Converts the whole string to uppercase/lowercase/first letter uppercase,rest lowercase. This function is applied AFTER the rename mask and after search&replace. Use the [U], [L] and [n] placeholders to convert only certain parts of the name to upper/lowercase!

    Opens a context menu with the following options:
    Load names from file: Specify a text file from which the new names should be loaded
    Edit names: Save current target names to a text file, edit them, and paste back.

Configure editor: Choose a different editor to edit the files. Make sure that the editor saves as plain text!

Define counter [C]    Allows to define the counter for the [C] field(s).
Start at:    Number of the first file. The files are always numbered the same way as they are shown in the result list. You can sort the result list just like in the main Total Commander window. Additionally you can reorder individual items using drag&drop, or Shift+Cursor keys.

Step by:    The counter is increased/decreased by this value.
Digits:    Width of the counter field. If digits is >1, the rename tool will insert leading zeros to get a fixed width number field.

F2 Load/save settings
    Allows to load or save the settings of the multi-rename tool.
<Default>    Sets the default settings (no changes to the names)
Save settings
    Allows to save the current settings
Delete entry    Deletes the last selected entry from the list

Entry names    Loads settings saved earlier

<File list header>    Allows to sort by old names, extensions, sizes or time stamps
<File list>    Shows a list of all files being renamed. The modified names are shown in the New name column. All changes to the above fields are immediately shown in this column, but the files are not actually renamed until the Start! button is pressed. If the rename mask contains an error, the string <Error!> is shown.

Individual items can be moved up/down with drag&drop or Shift+Cursor keys. This is useful for changing the file order for the counter function.

(next step)    Loads the rename results for the next rename step. Useful if you want to apply multiple rename rules to the same set of files. Shortcut: F5.
Start!    Starts to rename files. There will be a warning message if there are name conflicts.
Undo    Tries to undo the rename operation in reverse order (last renamed file first). This also works AFTER closing the Multi-Rename-Tool! Just re-open it with any file(s).

Result list    Creates a protocol of the renamed files.
Close    Closes the dialog box without any further actions.

Here is a description of all available placeholders. IMPORTANT: Upper/lowercase is relevant!
[N]    old file name, WITHOUT extension
[N2-5]    Characters 2 to 5 from the old name (totals to 4 characters). Double byte characters (e.g. Chinese, Japanese) are counted as 1 character! The first letter is accessed with '1'.
[N2,5]    5 characters starting at character 2
[N2-]    All characters starting at character 2
[N-8,5]    5 characters starting at the 8-last character (counted from the end of the name)

[N-8-5]    Characters from the 8th-last to the 5th-last character
[N2–5]    Characters from the 2nd to the 5th-last character
[N-5-]    Characters from the 5th-last character to the end of the name
[2-5]    Characters 2-5 from the whole name and extension (other numbers as in [N] definition)
[P]    Paste name of the parent directory, e.g. when renaming c:\directory\file.txt -> pastes "directory".
    Also working: [P2-5], [P2,5], [P-8,5], [P-8-5] and [P2-], see description of [N] above.

[G]    Grandparent directory (usage: see [P]).
[E]    Extension
[E1-2]    Characters 1-2 from the extension (other numbers as in [N] definition)
[C]    Paste counter, as defined in Define counter field
[C10+5:3]    Paste counter, define counter settings directly. In this example, start at 10, step by 5, use 3 digits width.
    Partial definitions like [C10] or [C+5] or [C:3] are also accepted.
[Caa+1]    Paste counter, define counter settings directly. In this example, start at aa, step 1 letter, use 2 digits (defined by 'aa' width)

[d]    Paste date as defined in current country settings. / is replaced by a dash
[Y]    Paste year in 4 digit form
[y]    Paste year in 2 digit form
[M]    Paste month, always 2 digit
[D]    Paste day, always 2 digit
[t]    Paste time, as defined in current country settings. : is replaced by a dot.
[h]    Paste hours, always in 24 hour 2 digit format
[m]    Paste minutes, always in 2 digit format
[s]    Paste seconds, always in 2 digit format

[U]    All characters after this position in uppercase
[L]    All characters after this position in lowercase
[F]    First letter of each word uppercase after this position, all others lowercase
[n]    All characters after this position again as in original name (upper/lowercase unchanged)
[[]    Insert square bracket: open
[]]    Insert square bracket: close (cannot be combined with other commands inside the square bracket!)
[=pluginname.fieldname.unit]

Insert field named "fieldname" from content plugin named "pluginname". "unit" may be an optional unit (if supported by that field), or a field formatter like YMD for date fields. You can use the [=?] Plugin button to insert plugin fields.
[=pluginname.fieldname.unit:4-7]
    Same as above, but for partial strings (here: letters 4-7).

 

Appendix B. Total Commander regex support.

Regular expressions are a very powerful search tool. They allow to search for complex classes of words. Regular expressions are mainly meant for professionals, but can also be useful in the office for finding certain documents (see examples below).

Total Commander supports regular expressions in the following functions:
– Commands – Search (in file name and file contents)
– In Lister
– In the Multi-Rename tool
– In the selection dialog

Regular expressions consist of normal characters and special characters, so-called meta-characters. The following characters are meta-characters or initial parts of meta-characters:
.  \  (  )  [  ]  {  }  ^  $  +  *  ?    (only in character classes: – )

Normal characters:

test    finds the string "test" in the searched text. Note: This finds "test" ANYWHERE in a file name or on a line in text.

Escape sequences:

A backslash \ starts an Escape sequence. Examples for escape sequences:

\t    Tabstop
\xnn    Character with hexadecimal code nn. Example: \x20 is the space character. The character table charmap.exe (if installed) shows the character code of most special characters. You can use the Windows calculator in scientific mode to convert from decimal to hex.
\[    Left square bracket. Since the square brackets are meta-characters, they need to be written as \[ to search for them in the target string.
\\    Finds a backslash.
\.    Finds a dot ("." alone finds any character, see below).

Character classes

Characters in square brackets build a character class. It will find exacly one character from this class. A dash allows to define groups, e.g. [a-z]. A ^ at the beginning finds all characters except for those listed.
Examples:

[aeiou]    Finds exactly one of the listed vovels.
[^aeiou]    Finds everything except for a vovel.
M[ae][iy]er    Finds a Mr. Meier in all possible ways of writing: Mayer, Meyer, Maier, Meier. Very useful if you cannot remember the exact writing of a name.

Meta-characters

Here is a list of the most important meta-characters:

^    Line start
$    Line end
.    Any character
\w    a letter, digit or underscore _
\W    the opposite of \w
\d    a digit
\D    no digit
\s    a word separator (space, tab etc)
\S    no word separator
\b    finds a word boundary (combination of \s and \S)
\B    the opposite of \b

Iterators

Iterators are used for a repetition of the character or expression to the left of the iterator.
*    zero or more occurances
+    one or more occurances
{n}    exactly n occurances
{n,}    at least n occurances

{n,m}    at least n and max. m occurances

All these operators are "greedy", which means that they take as many characters as they can get. Putting a question mark ? after an operator makes it "non-greedy", i.e. it takes only as many characters as needed.
Example: "b+" applied to the target string "abbbbc" finds "bbbb", "b+?" finds just "b".

Alternatives

Alternatives are put in round braces, and are separated by a vertical dash.

Example: (John|James|Peter)  finds one of the names John, James or Peter.

Subexpressions for search+replace

Text parts in round braces are taken as subexpressions.
Example: To swap the title and interpret in the file name of an mp3 file, when they are separated by a dash (Title – Interpret.mp3), this can be solved like this:
Search for: (.*) – (.*)\.mp3
Replace by: $2 – $1.mp3
Here $1 means the text in the first brace, and $2 the text in the second brace.

Backreferences

\n    Finds subexpression n another time in the search result.

Example: (.+)\1+  finds e.g.  abab  (where the first ab is found by .+  and the second by \1+ )

Modifiers

Modifiers are used for changing behaviour of regular expressions.

(?i)    Ignore Upper-/lowercase. In Total Commander, this is the default for file names.
(?-i)    Case-sensitive matching.
(?g)    Switches on "greedy" mode (active by default)
(?-g)    Turns off "greedy" mode, so "+" means the same as "+?"

The other modificators are not relevant for Total Commander, because the program only supports searching within one line.

Total Commander uses the free Delphi library TRegExpr by Andrey V. Sorokin: http://www.regexpstudio.com/
Some of the above explanations are from the help file for this library.

● ● ●
Artem Russakovskii is a San Francisco programmer and blogger. Follow Artem on Twitter (@ArtemR) or subscribe to the RSS feed.

In the meantime, if you found this article useful, feel free to buy me a cup of coffee below.