i need to make files name string like text_000.txt, where 000 should increase every file but i got problem like this where after 10 iterations the digits in the file name become more than necessary
i need to get file name like "text_001.mp3" and it's ok for 9 files, but after i got "text_0010"
this is code
*param (
[string]$dir = "C:\Users\Администратор\Desktop\music"
)
Set-Location -Path $dir
$count=1
Get-ChildItem -Path $dir | Sort-Object -Property LastWriteTime | % { Rename-Item $_ -NewName (‘text' '{0}'.PadLeft(5,"0") -f $count '.txt') }*
CodePudding user response:
It is probably easier to directly format your filename number with leading zeros, like:
'text_{0:000}.txt' -f 17
text_017.txt
Also knowing that this will properly overflow (prevent duplicate filenames):
'text_{0:000}.txt' -f 1234
text_1234.txt
In your case:
|% { Rename-Item $_ -NewName ('text_{0:000}.txt' -f $count ) }
CodePudding user response:
Why don't you just use PadLeft method like this:
'Voina_string' {0}.PadLeft(3, '0')
3 is the number of maximum length the {0} string can/should be. And you fill it with '0' or any other symbol.
So after your edit:
param ( [string]$dir = "C:\Users\Администратор\Desktop\music" ) Set-Location -Path $dir $count=1 Get-ChildItem -Path $dir | Sort-Object -Property LastWriteTime | % { Rename-Item $_ -NewName (‘Война_и_мир_Часть_' '{0}'.PadLeft(3,'0') -f $count '.txt') }