Greg

Mass Renaming Files via Command Prompt

Source: http://superuser.com/questions/16007/how-can-i-mass-rename-files-from-the-command-line-or-using-a-3rd-party-tool
Thanks to zdan of superuser.com

tl;dr? Here’s the script:

dir /B > fileList.txt
for /f “tokens=1,2,3,4,5 delims=-” %i in (fileList.txt) DO ren “%i-%j-%k-%l-%m” “%i-flowers-%l-%m”
rm fileList.txt 

Enjoy. Now for the curious, read on. 😀

What those commands do are as follows:

  1. Create a list of the files in your current folder as a text file.
  2. Loops through the list of files and renames them.
  3. Deletes the text file so you’re left with your deliciously renamed files en masse.

Now, the way the loop works is really cool, and I mean REALLY cool. Here’s what happens:

  • It loops through every line in the file we just created, passing it to the rename command.
  • So then for every line, the rename command then “tokenizes” the line based on the delimiters, AKA separators.
  • Renames the file based on these temporary tokens to the desired string of text and/or arrangement of tokens.

So, now we can gather what info the script is using:

Tokens: 5, %i, %j, %k, %l, %m
Delimiters: –
Current File Name Pattern: “%i-%j-%k-%l-%m”
Desired File Name Pattern“%i-flowers-%l-%m”

Now we know, after that close look at what the command does, that the script is using 5 tokens, what their variables are, how it’s delimiting the file name, what the file name pattern currently is, and how it will be renamed. I told you it was neat. It certainly makes life easier when you’re handed a batch of files improperly renamed ever-so-slightly.  🙂 Enjoy.