This question already has an answer here:
- PowerShell: Quoting -replace & variables 4 answers
I am trying to replace filenames in a given folder, but using regular expressions as a filter and in the new filenames, in PowerShell.
For example, a file name "CEX-13" should be renamed to "C-0013"; "CEX-14" should change to "C-0014", etc.
I have this, but I get an error that I cannot create a file that already exists.
My current code is:
foreach ($file in get-childitem | where-object {$_.name -match "^CEX-(\d\d)"})
{
rename-item $file -newname ($file.name -replace "^CEX-(\d\d)", "C-00$1")
}
Any help will be greatly appreciated.