Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 473
ofer.sheffer Avatar asked Jan 02 '14 00:01

ofer.sheffer


People also ask

How do I rename multiple files with sequential numbers in Windows?

Quick tip: Alternatively, you can use the "Ctrl + A" keyboard shortcut to select all files. You can press and hold the Ctrl key and then click each file to rename. Or you can choose the first file, press and hold the Shift key, and then click the last file to select a group.

How do I rename a bunch of files with different names?

Rename in File Explorer To rename multiple files from File Explorer, select all the files you wish to rename, then press the F2 key. The name of the last file will become highlighted. Type the new name you wish to give to every file, then press Enter.

How do I add a suffix in Windows?

In Windows 10: In the Internet Protocol (TCP/IP) Properties dialog box, click the Advanced button. Click the DNS tab in the Advanced TCP/IP Settings dialog box. Click the "Append these DNS suffixes (in order)" radio button.


2 Answers

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.

like image 96
ofer.sheffer Avatar answered Sep 20 '22 15:09

ofer.sheffer


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.

like image 31
Dave.Gugg Avatar answered Sep 16 '22 15:09

Dave.Gugg