Renaming sequenced files in PowerShell

I’ve been scanning pages from a paperback book (by the way, did you know that Kinko’s / Fedex stores often have a paper cutter capable of cutting the spine off a paperback book, making it very easy to scan those pages?).

My new HP scanner will save files in a sequence – but each time I scan, I have to figure out to save the files so that the file name matches the page number.

For example, I started by scanning pages 18 through 29, and I saved the first file as “Page018.png”, hoping that the subsequent pages would be saved as “Page019.png”, “Page020.png”…”Page029.png”

However, what I got was “Page018_0001.png”, “Page018_0002.png”…”Page018_0012.png”

I used PowerShell to rename the files –

> $pages = Get-ChildItem c:\scans\1
> $pages |
  Rename-Item -NewName {$_.Name -replace "\d\d_\d{4}",
           [string](17 + [int]($_.Name).Substring(9,3)) }

For the next batch, I scanned pages 32 through 54, and saved the first one as “Page.png” – the pages were saved as “Page_0001.png”, “Page_0002.png”…”Page_0023.png”

This time, I had to use the following commands –

> $pages = Get-ChildItem c:\scans\1
> $pages | 
   Rename-Item -NewName {$_.Name -replace "_\d{4}", 
            ('00' + [string](31 + [int]($_.Name).Substring(7,2))) }

I do wish I could save these with the right sequence start and not have to rename them.

Other than this quirk, I confess I’m enjoying this HP device. It’s got its own wireless NIC, so I can scan directly to my home file server, and I don’t have to mess with USB on my laptop.

Leave a Reply

Your email address will not be published. Required fields are marked *