Search code examples
powershellrenamesequential

Renaming pdf files in sequential order using powershell


I have a large number of pdf's that need renamed in sequential order. These were originally scanned into a single document, then extracted as separate files. When extracted, the name becomes "444026-444050 1", "444026-444050 2", etc. I am trying to rename all the files to match the document number ("444026-444050 1" would become "444026").

I found the following line of code that I can use in Powershell, but it seems that anything over 9 files there is a problem! Once I try it with 10 files, only the first file is saved correctly. The rest become jumbled (file 444027 has the contents of file 444035, then file 444028 has 444027, and 444029 has 444028, etc.)

I imagine there is some sort of problem with a loop, but am having difficulty fixing it.

Can someone help? thanks

Dir *.pdf | ForEach-Object  -begin { $count=26 }  -process { rename-item $_ -NewName "4440$count.pdf"; $count++ }

Solution

  • The order in which Dir (which is an alias for Get-ChildItem) retrieves the items does not appear to be strictly guaranteed. Furthermore, if it is sorting it's probably sorting them as strings and "444026-444050 10" comes before "444026-444050 2" as strings. It might be worth inserting SortObject into your pipeline and using Split to get at the sequence number you care about:

    Dir *.pdf | Sort-Object -Property {[int]$_.Name.Split()[1].Split(".")[0]} | ForEach-Object  -begin { $count=26 } -process { rename-item $_ -NewName "4440$count.pdf"; $count++ }
    

    The key part is this new pipeline stage inserted after Dir and before ForEach-Object:

    Sort-Object -Property {[int]$_.Name.Split()[1].Split(".")[0]}
    

    This says to sort the output of Dir according to the whatever comes between the first space and the subsequent period, comparing those things as integers (not strings). This ensures that your results will be ordered and that you'll get them in numeric order, not lexicographic order.