Rename multiple files in a folder, add a prefix (Windows)
I'd like to batch rename files in a folder, prefixing the folder's name into the new names. i.e. files in C:\house chores\
will all be renamed house chores - $old_name
.
Solution 1:
Option 1: Using Windows PowerShell
Open the windows menu. Type: "PowerShell" and open the 'Windows PowerShell' command window.
Goto folder with desired files: e.g. cd "C:\house chores" Notice: address must incorporate quotes "" if there are spaces involved.
You can use 'dir' to see all the files in the folder. Using '|' will pipeline the output of 'dir' for the command that follows.
Notes: 'dir' is an alias of 'Get-ChildItem'. See: wiki: cmdlets. One can provide further functionality. e.g. 'dir -recurse' outputs all the files, folders and sub-folders.
What if I only want a range of files?
Instead of 'dir |' I can use:
dir | where-object -filterscript {($_.Name -ge 'DSC_20') -and ($_.Name -le 'DSC_31')} |
For batch-renaming with the directory name as a prefix:
dir | Rename-Item -NewName {$_.Directory.Name + " - " + $_.Name}
Option 2: Using Command Prompt
In the folder press shift+right-click : select 'open command-window here'
for %a in (*.*) do ren "%a" "prefix - %a"
If there are a lot of files, it might be good to add an '@echo off' command before this and an 'echo on' command at the end.
Solution 2:
The problem with the two Powershell answers here is that the prefix can end up being duplicated since the script will potentially run over the file both before and after it has been renamed, depending on the directory being resorted as the renaming process runs. To get around this, simply use the -Exclude
option:
Get-ChildItem -Exclude "house chores-*" | rename-item -NewName { "house chores-" + $_.Name }
This will prevent the process from renaming any one file more than once.
Solution 3:
Free Software 'Bulk Rename Utility' also works well (and is powerful for advanced tasks also). Download and installation takes a minute.
See screenshots and tutorial on original website.
--
I cannot provide step-by-step screenshots as the images will have to be released under Creative Commons License, and I do not own the screenshots of the software.
Disclaimer: I am not associated with the said software/company in any way. I liked the product for my own task, it serves OP's and similar requirements, thus recommending.