I have a list of files like this:
Test.(file1)[text].txt
Test.(file2)[text].txt
Test.(file3)[text].txt
I need to remove any "()","[]" and any ".", replace them spaces without changing the file extension. I've tried the following but it's not working:
dir C:\scratch\*.txt | % { Rename-Item $_ ($_.basename -replace "\(", " "`
-replace "\)", " "`
-replace "\[", " "`
-replace "\]", " "`
-replace ".", " ")}
Ideally the file name should end up in the format "Test file1 text.txt"
IMO Rename-Item appears to be broken in terms of its handling of paths with [ ] chars in them. Fortunately, Move-Item works:
dir *.txt |
mi -dest {($_.Basename -replace '[()[\].]+',' ').Trim() + $_.Extension}
Where mi
is the alias for Move-Item. Note that a number of cmdlets like Rename-Item and Move-Item can be piped to directly and then their NewName/Destination parameters can take scriptblocks in which you can use the pipeline object. Internally this works for pipeline bound parameters as PowerShell will execute the scriptblock and attempt to coerce the result to the type expected by the parameter.
Rename-Item doesn't work with files that contain "[" or "]". This is a known issue.
It turns out that the problem is because Rename-Item is missing a -LiteralPath parameter. Move-Item has this parameter, so you can use it to do what you want:
ls C:\scratch\*.txt |
% { Move-Item -literalpath $_.fullname `
($_.name -replace "[()\[\]]|\.(?!\w{3}$)", " ") -whatif }
Remove the -WhatIf parameter when it looks like it is doing what you want it to do. My regular expression leaves files like "Test file1 text.txt", which isn't quite what you wanted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With