question

KhurramAnsari-5263 avatar image
0 Votes"
KhurramAnsari-5263 asked KhurramNesar-7287 commented

Need to omit the leading characters

Hi I am very new to PowerShell, and I need to rename multiple files in PS.

I have these file:

1- 000234809.jpg
2- 000678380.jpg
3- 000539010.jpg

I need to rename these files in PowerShell. The new file names should be like this:

1- 234809.jpg
2- 678380.jpg
3- 539010.jpg

Note: I need to remove only first 3 zeros from the file name. Thank you all.

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered KhurramAnsari-5263 commented

Hi @KhurramAnsari-5263 ,

please try this to start with the script:

 $filePath = "C:\Junk\1" # get files
 Get-ChildItem -Path $filePath | ForEach-Object {
     $b = $_ -replace ('^000', '') # replace leading 000
     $_ | Rename-Item -NewName { $b } # rename file
 }

This is the result:

 234809.jpg
 678380.jpg
 53900010.jpg
 539010.jpg


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten



· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Andreas I tried this script, it should fix the 000 in the files.
But its is changing my file to this name, I also have 0000 (four leading files in this folder as well)

1- 000791309.jpg-
2- 000048394.jpg-

$filePath = "C:\my-pic-folder" # get files
Get-ChildItem -Path $filePath | ForEach-Object {
$a = "$".Split("- ") # split at "- "
$b1 = $a[2] -replace ('^000', '') # replace leading 000
$b = $a[0] + "- " + $b1 # build new name
$
| Rename-Item -NewName { $b } # rename file
}


Second Script is not changing anything.

Thanks so much for your response.

0 Votes 0 ·

Hi @KhurramAnsari-5263 ,

just to verify;
There is a space after the - in every file name? At least in your question and in in your comment there is a space in the file name and the script is working with - followed by a space.

In your question you wrote:

Note: I need to remove only first 3 zeros from the file name. Thank you all.

In your comment you wrote:

I also have 0000 (four leading files in this folder as well)

You want only the first 3 zeros removed, even if there are 4 leading zeros?


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten



0 Votes 0 ·

Hi Andreas,
Thanks again. I thing there is a file name confusion.

my jpg file name is only this 000791309.jpg

I used ("1- 2- 3-" before the file name) for the my excel sheet number. 1- 2- is not part of the actual file name. (I am sorry I shouldn't be writing those (1- 2-) in the question.

0 Votes 0 ·

I just renamed my file name to this 1- 000234809.jpg and your script was giving me 1- 234809.jpg (which is awesome! great job!)

My file name is only this 000234809.jpg

Yes I do have 0000765870.jpg in my folder, but once I know how to remove 000 zeros I can apply the same rule for the 0000 zeros.

Nice work!

0 Votes 0 ·
Show more comments

Done! Great.

Thanks Andreas, you all are the best!

If you possible, please tell me any nice and "easy PowerShell book". I really want to learn PowerShell like you guys, I don't mind investing some money in this.

Thanks again!

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered KhurramNesar-7287 commented

Here's a variation on the answer by @AndreasBaumgarten. This one removes the zeros without regard for how many of them there are. In other words, it renders the string "000000123" as if it were being displayed as the integer value "123".

 $filePath = "C:\Junk\1" # get files
 Get-ChildItem -Path $filePath |
     ForEach-Object{
         $r = $_.Name -replace '^0+(\d+?\..+)$','$1'
         if ($_.Name -ne $r){
             $_ | Rename-Item -NewName $r # rename file
         }
     }
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Rich,
Thanks for the help.

My file name is only this:

000234809.jpg
000678380.jpg
000539010.jpg

0 Votes 0 ·

That's easy enough to fix. See the modified script in my original answer.

If your file names don't start with one or more digits followed by a hyphen and a space why post them with those characters in your description of the problem??

You know the old acronym and adage: GIGO ( Garbage In, Garbage Out )! A bad problem description results in a bad problem resolution! :-)



0 Votes 0 ·

LoL very true. Garbage in Garbage out

I agree...:)

0 Votes 0 ·

Great, this one also worked!

Thanks Rich. If you know any nice and easy PowerShell book, kindly let me know.

Thank you all for the help.

0 Votes 0 ·

Here ya go:

Windows-PowerShell-4

It's free. It covers version 4, but the differences between that and version 5 are trivial, especially at an early stage of learning. The 1st half covers what you need to know about the language. The 2nd half covers typical applications of the language to common tasks.

0 Votes 0 ·

Wonderful. Thanks again Rich!!

0 Votes 0 ·