Mass Renaming Files via Command Prompt
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”
Enjoy. Now for the curious, read on. 😀
What those commands do are as follows:
- Create a list of the files in your current folder as a text file.
- Loops through the list of files and renames them.
- 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
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.