I have .mp3 files in my folder with these names: 2.mp3,4.mp3,6.mp3,8.mp3,10.mp3,12.mp3,...102.mp3,104.mp3... and would like to rename them all and get following result: 0001.mp3,0002.mp3,0003.mp3,0004.mp3...
the following code changes the names of all files, but the problem is that it does not change/sort them in the exact original order as they are originally displayed in my folder.
$i = 1
Get-ChildItem *.mp3 | %{Rename-Item $_ -NewName ('{0:D4}.mp3' -f $i++)}
Something like this written below I want to achieve
- 2.mp3 -> 0001.mp3
- 4.mp3 -> 0002.mp3
- 6.mp3 -> 0003.mp3
- 8.mp3 -> 0004.mp3
- 10.mp3 -> 0005.mp3
- 12.mp3 -> 0006.mp3
- ...
I have currently come to this half-solution that does not work and I do not know how to proceed/solve this problem.
$i = 1
Get-ChildItem *.mp3 | %{Rename-Item $_ -NewName {'{0:D4}.mp3' -f [int]($_.BaseName -replace '\D') $i++ }}
$i++
from your half-solution and it should work. – zett42G-CI
call to an array and then use the index number plus1
to derive the number. – Lee_Dailey