Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rename files with Static text after removing '0'

I have few files starting with zero like this

000145
000029

I am able to remove the zero for the file name with this script and also want to add _16 in the filename while renaming and the file should be like this 145_16 or 29_16.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$files = Get-ChildItem -File -Filter 0*
$files |Rename-Item -NewName { $_.Name.TrimStart('0')+"_16"}

But after renaming the file is showing the .jpg also in the filename. Here is the example

145.jpg_16 or 29.jpg_16

Can anyone please help me to resolve this

>Solution :

The .Name property includes the filename extension. Use .BaseName and .Extension separately:

$files | Rename-Item -NewName { $_.BaseName.TrimStart('0')+'_16' + $_.Extension }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading