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
.
This question is related to
windows
powershell
filenames
prefix
batch-rename
Based on @ofer.sheffer answer this command will mass rename and append the current date to the filename. ie "file.txt" becomes "20180329 - file.txt" for all files in the current folder
for %a in (*.*) do ren "%a" "%date:~-4,4%%date:~-7,2%%date:~-10,2% - %a"
Based on @ofer.sheffer answer, this is the CMD variant for adding an affix (this is not the question, but this page is still the #1 google result if you search affix). It is a bit different because of the extension.
for %a in (*.*) do ren "%~a" "%~na-affix%~xa"
You can change the "-affix" part.
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.
This worked for me, first cd in the directory that you would like to change the filenames to and then run the following command:
Get-ChildItem | rename-item -NewName { "house chores-" + $_.Name }
I know it's an old question but I learned alot from the various answers but came up with my own solution as a function. This should dynamically add the parent folder as a prefix to all files that matches a certain pattern but only if it does not have that prefix already.
function Add-DirectoryPrefix($pattern) {
# To debug, replace the Rename-Item with Select-Object
Get-ChildItem -Path .\* -Filter $pattern -Recurse |
Where-Object {$_.Name -notlike ($_.Directory.Name + '*')} |
Rename-Item -NewName {$_.Directory.Name + '-' + $_.Name}
# Select-Object -Property Directory,Name,@{Name = 'NewName'; Expression= {$_.Directory.Name + '-' + $_.Name}}
}
https://gist.github.com/kmpm/4f94e46e569ae0a4e688581231fa9e00
I was tearing my hair out because for some items, the renamed item would get renamed again (repeatedly, unless max file name length was reached). This was happening both for Get-ChildItem and piping the output of dir. I guess that the renamed files got picked up because of a change in the alphabetical ordering. I solved this problem in the following way:
Get-ChildItem -Path . -OutVariable dirs
foreach ($i in $dirs) { Rename-Item $i.name ("<MY_PREFIX>"+$i.name) }
This "locks" the results returned by Get-ChildItem in the variable $dirs and you can iterate over it without fear that ordering will change or other funny business will happen.
Dave.Gugg's tip for using -Exclude should also solve this problem, but this is a different approach; perhaps if the files being renamed already contain the pattern used in the prefix.
(Disclaimer: I'm very much a PowerShell n00b.)
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.
Source: Stackoverflow.com