Rename several files in the folder, add the prefix (Windows)

I would like to batch rename files in a folder by adding the folder name to the new names. those. files in C:\house chores\ will be renamed to house chores - $old_name .

+56
windows powershell filenames pipeline prefix batch-rename
Jan 02
source share
8 answers

Option 1: Using Windows PowerShell

Open the window menu. Type: "PowerShell" and open a Windows PowerShell command prompt.

Open the folder with the necessary files: for example. cd "C: \ house chores" Note: the address must contain quotation marks "" if there are spaces.

You can use 'dir' to view all the files in a folder. Using '|' there will be pipelined output "dir" for the next command.

Notes: 'dir' is an alias of 'Get-ChildItem'. See: wiki: cmdlets . Additional functionality can be provided. for example, 'dir -recurse' displays all files, folders, and subfolders.

What if I need only a series 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 a directory name as a prefix:

 dir | Rename-Item -NewName {$_.Directory.Name + " - " + $_.Name} 

Option 2. Using the command line

In the folder, press shift + right-click: select "open command window here"

 for %a in (*.*) do ren "%a" "prefix - %a" 

If there are many files, it might be nice to add the "@echo off" command before this and the "echo on" command at the end.

+111
Jan 02
source share

The problem with the two Powershell answers here is that the prefix can end up being duplicated, as the script could potentially run on the file both before and after it was renamed, depending on which directory is restored during the renaming process . To get around this, simply use -Exclude :

 Get-ChildItem -Exclude "house chores-*" | rename-item -NewName { "house chores-" + $_.Name } 

This will prevent the process from renaming any one file more than once.

+24
May 17 '16 at 15:10
source share

Bulk Rename Utility ' freeware also works well (and is also powerful for advanced tasks). Download , and installation takes a minute.

See screenshots and tutorial on the original website.

-

I cannot provide step-by-step screenshots because the images must be released under the Creative Commons License , and I do not own screenshots of the software.

Disclaimer: I am in no way associated with the specified software / company. I liked the product for my own task, it caters for the OP and similar requirements, recommending it this way.

+12
Mar 24 '14 at 8:50
source share

This worked for me, first cd in the directory where you want to change the file names, and then run the following command:

 Get-ChildItem | rename-item -NewName { "house chores-" + $_.Name } 
+11
Aug 23 '14 at 16:59
source share

Based on @ ofer.sheffer's answer, this command will be renamed and add the current date to the file name. that is, "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" 
+1
Mar 29 '18 at 15:03
source share

I tore my hair because for some elements the renamed element was renamed again (repeatedly if the maximum length of the file name was not reached). This happened for both Get-ChildItem and for passing dir output. I assume that the renamed files were matched due to a change in alphabetical order. I solved this problem as follows:

 Get-ChildItem -Path . -OutVariable dirs foreach ($i in $dirs) { Rename-Item $i.name ("<MY_PREFIX>"+$i.name) } 

This "blocks" the results returned by Get-ChildItem in the $ dirs variable, and you can iterate over it without fear that the order will change or other funny work will happen.

Dave.Gugg's advice on using -Exclude should also solve this problem, but this is a different approach; perhaps if the renamed files already contain the template used in the prefix.

(Disclaimer: I am very similar to PowerShell n00b.)

+1
Nov 02 '18 at 11:47
source share

I know this is an old question, but I learned a lot from different answers, but as a function I proposed my own solution. This should dynamically add the parent folder as a prefix to all files that match a specific template, but only if it does not already have this prefix.

 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

0
Jan 04 '18 at 16:34
source share

Based on @ ofer.sheffer's answer, this is a CMD option for adding an affix (this is not a question, but this page is still Google's # 1 result if you're looking for an affix). This is slightly different due to expansion.

 for %a in (*.*) do ren "%~a" "%~na-affix%~xa" 

You can change the "-affix" part.

0
Apr 05 '19 at 9:28
source share



All Articles