windowspowershellfilenamesprefixbatch-rename

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

  • 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.