Search code examples
windowspowershellcmdfile-rename

how can i recursively rename folders in windows matching string and ignoring case


I have subfolders which have names:

  • original_Optimize
  • Original_optimize
  • original_optimize
  • Original_Optimize

I would like to rename all of these to:

  • Original_Optimize

Is there an easy way of doing this in windows (perhaps using powershell or something in command prompt ) ?


Solution

  • You can do that in two Rename-Item calls. The first would add a prefix to each name to avoid the 'Source and destination path must be different.' error. The second run will remove the prefix.

    Get-ChildItem -Filter original_optimize -Recurse | 
    Rename-Item -NewName __foo__Original_Optimize -PassThru | 
    Rename-Item -NewName {$_.Name -replace '^__foo__'} -PassThru