0

In this question, How do I rename part of a filename, if the newly renamed filename already exists? Want the newly attempted filename to overwrite the existing file. Also want to rename folders within folders within folders.

Replace "Default" with "VOD" in filename,

Replace Part of File Name Powershell

Get-ChildItem Default_*.csv |
    Rename-Item -NewName {$_.Name -replace '^Default','VOD'}

ls *.csv | Rename-Item -NewName {$_.Name -replace "Default","VOD"}

Solution here was not working with -Force:

Get-ChildItem *.* -File -Recurse |
    Rename-Item -NewName {$_.Name -replace 'Default','VOD'} -Force
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328

1 Answers1

0

As per this question and answer: rename-item and override if filename exists

Use Move-Item and -Destination, and also swap $_.Name for $_.FullName (if you don't do that it will throw everything into your current directory, because Name is just the name of the file, not the full path). Check using -WhatIf to see what operations will be carried out, just in case:

With -WhatIf:

Get-ChildItem *.* -File  -Recurse |
    Move-Item -Destination {$_.FullName -replace 'Default','VOD'} -WhatIf

Output:

What if: Performing the operation "Move File" on target "Item: C:\temp\pstest\Default_77.txt Destination: C:\temp\pstest\VOD_77.txt".

Now with -Force:

When you're comfortable with the output from using the -WhatIf parameter, change it to -Force to complete the file move.

Get-ChildItem *.* -File  -Recurse | 
    Move-Item -Destination {$_.FullName -replace 'Default','VOD'} -Force
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
mjsqu
  • 5,151
  • 1
  • 17
  • 21