5

I am using PowerShell to replace pdf file names:

Get-ChildItem -Path C:\All -Filter *.pdf | Rename-Item -NewName { $_.Name.Replace('.','_') }

It works fine if I replace %20 to _, but if we use "test.one.pdf", I need "test_one.pdf" as output, if I use above syntax it replaces the . before pdf like "test_one_pdf".

1
  • @4c74356b41 He doesn't use regex, anyhow I don't see a reason why this doesn't work. Mar 29, 2017 at 10:03

1 Answer 1

13

Your code will rename name.001.pdf to name_001_pdf, so unless you actually want to remove the extensions of your files you need to replace only within the files name and not the extension.

Get-ChildItem -Path "C:\All" -Filter "*.pdf" | Rename-Item -NewName { $_.BaseName.Replace(".","_") + $_.Extension }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.