Renaming Sequenced Files in PowerShell – part 2

Since last month, I’ve made a small change to the way I rename the files produced by my scanner.

I wrote the following short function:

function nuname ($n) {
   [void]($n -match "Page(\d+)_(\d+)")
   return "Page" + [string]([int]$matches[1] + [int]$matches[2]) + ".png"
}

And now, instead of naming files “Page ###”, I simply export them from the HP scanner dialog using the number of the page before the first page I scanned. The HP scanner software adds a sequence number starting with 1, so my function adds the number I supplied and the sequence number from the software, then fills that in as the page number in the resulting file name.

For example, if I start scanning a number of pages starting with page 180 from the book, I save the bunch as
179.png. The HP scanner software saves that page as 179_1.png, and my function turns that into Page180.png
.

Of course, it would probably make more sense to name the bunch with the actual page number of the first page scanned, in which case the function would have to be changed slightly:

function nuname ($n) {
   [void]($n -match "Page(\d+)_(\d+)")
   return "Page" + [string]([int]$matches[1] + [int]$matches[2] - 1) + ".png"
}

Then, when I scanned a bunch starting with Page 180, I would save the bunch as 180.png, the first one would be saved (by the scanner software) as 180_1.png, and my function would change it to Page180.png. Much more intuitive.

Leave a Reply

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