Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace "." with "_" in file name

Tags:

powershell

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".

like image 702
sona Avatar asked Dec 10 '22 12:12

sona


1 Answers

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 }
like image 118
henrycarteruk Avatar answered Dec 15 '22 00:12

henrycarteruk