0

I'm trying to change the names of 561 images using powershell. The first and second changes met my expectations, but when I tried to undo the second change a third time, the results did not meet my expectations.

here is my code:

PS E:\PART_5> $a = 1
PS E:\PART_5> Get-ChildItem | ForEach-Object {
>> $name = "newName_$a" + $_.Extension
>> Rename-Item -Path $_.FullName -NewName $name
>> $a = $a + 1
>> }

PS E:\PART_5> $a = 1
PS E:\PART_5> Get-ChildItem | ForEach-Object {
>> $name = "newestName_$a" + $_.Extension
>> Rename-Item -Path $_.FullName -NewName $name
>> $a = $a + 1
>> }

PS E:\PART_5> $a = 1
PS E:\PART_5> Get-ChildItem | ForEach-Object {
>> $name = "newName_$a" + $_.Extension
>> Rename-Item -Path $_.FullName -NewName $name
>> $a = $a + 1
>> }

and results:

First execution result Second execution result Third execution result
newName_1.jpg newestName_1.jpg newName_1000.jpg
newName_2.jpg newestName_2.jpg newName_1001.jpg
... ... ...
newName_561.jpg newestName_561.jpg newName_1560.jpg

To test I output the variable "a" in the process and the most amazing thing happened.

PS E:\PART_5> $a = 1
PS E:\PART_5> Get-ChildItem | ForEach-Object {
>> $name = "newestName_$a" + $_.Extension
>> Rename-Item -Path $_.FullName -NewName $name
>> $a
>> $a = $a + 1
>> }

PS E:\PART_5> $a = 1
PS E:\PART_5> Get-ChildItem | ForEach-Object {
>> $name = "newName_$a" + $_.Extension
>> Rename-Item -Path $_.FullName -NewName $name
>> $a
>> $a = $a + 1
>> }

When I loop through these two pieces of code, I find that "newestName" is always correct, looping "a" from 1 to 561; and "newName" is always wrong, and looping from 1 to 1560.

enter image description here

I don't know what the problem is and I hope someone can tell me.

10
  • 1
    And you're 100% sure you didn't accidentally set $a = 1000 before the third loop? It's also quite strange that you have 561 files during the first 4 attempts and then suddenly 1561 files in the last attempt... Can you do Get-ChildItem E:\PART_5 |Measure to test how many files/subfolders you actually have in the directory? Commented Feb 8 at 14:07
  • @MathiasR.Jessen Of course, I'm sure there's nothing wrong
    – u73a5
    Commented Feb 8 at 14:10
  • 4
    Since you are note using any filter on Get-ChildItem, files that already have been renamed can be picked up again and again and again.. Either capture the result of Get-ChildItem in a variable first and then loop over that OR enclose the cmd in between brackets (Get-ChildItem) so the command will finish collecting file and foldernames before it enters the loop.
    – Theo
    Commented Feb 8 at 14:48
  • 1
    Thanks, I seem to understand. I used to think that the command connected by the pipeline will start the next command after the execution of the previous one is completed, but in fact, it does not wait for the previous command to complete, and the next command will start executing when it has output. Can I understand it this way?
    – u73a5
    Commented Feb 8 at 15:02
  • 1
    @并鱼厥切 No, the pipeline goes back and forth one item at a time.
    – js2010
    Commented Feb 8 at 15:15

0

Browse other questions tagged or ask your own question.