Original file names:
XYZ_150014_0101_ABC_01_20150404_FD_v03 XYZ_150014_0101_ABC_01_20150411_FD_v03 XYZ_150014_0101_ABC_01_20150418_FD_v02 XYZ_150014_0101_ABC_01_20150425_FD_v02
I need to update "0101" to "01 + [2 digit increment value]", so it should look like the list below:
XYZ_150014_0101_ABC_01_20150404_FD_v03 XYZ_150014_0102_ABC_01_20150411_FD_v03 XYZ_150014_0103_ABC_01_20150418_FD_v02 XYZ_150014_0104_ABC_01_20150425_FD_v02
Below is the code that I am using:
$i = 1
Dir|Rename-Item –NewName {$_.name –replace "0101",("01" + "{0:D2}" -f $i);$i=++$i}
Unfortunately, this only updates "0101" to "0102", leaving behind the incremented values.
I have also tried the script below and come up with the same response as the script above.
$i = 1
Dir|Rename-Item –NewName {$_.name –replace "0101",("01{0:D2}" -f $i++)}
What am I missing to cause the increment to occur?
This is a variable scope issue: Have a look at Get-Help About_Scope. Use the script scope and you should be fine.
$i = 1
Dir xyz*|Rename-Item –NewName {$_.name –replace "0101",("01{0:D2}" -f $script:i++)}