Wednesday, January 29, 2014

A Script For When Your Duplexing Scanner is Broken

I do a lot of scanning.  I've got a whole lot more to do.   Efficiency is getting to be important.  Starting years ago with a cheap flatbed and not a lot of disk (when my scans were terrible,) I've come some distance, owning a few decent scanners with high-speed ADFs and duplexing ability.  But there's a thing about duplexers - they're often broken.  Not just offline, unusable, smoking and shooting gears all over, but subtly broken in ways you won't discover until page 305 of the last existing copy of your document resembles a toilet paper roll after the cat's been at it.  Or they just jam.  Psuedo-jam, showing no apparent problem with the paper or the rollers or the little gnomes inside that flip the paper over.  They're just feeling moody and have decided that tonight, you're better off with NetFlix and the cat.

So you write that feature off - the ADF still works in simplex mode, right?  Usually they do.  So you scan one side of your stack and then the other side.  If you're using a directly-connected scanner and a program like Acrobat, you've got the benefit of a duplexing mode that will take care of collating and ordering the pages to mirror the original document.  If you're scanning directly to a stack of image files, either because you're using a network scanner that can only do that, you want to massage the images before sticking them into Acrobat or you're just using a different set of tools altogether, you'll need to rename the two sets of files ("front" and "back," aka "odd" and "even") into one sequential set to feed in proper order into your favorite PDF-maker.

That's where I was tonight, so I've written a VBscript to do the job.  IrfanView has a batch mode which will do this but I found setup too tedious and too easy (for me) to make mistakes.  This problem has surely been solved many times over but my google searches for extant examples were in vain tonight (mainly hindered by the number of "(web)page scanning scripts" out there.)  I make no claims at any talent for scripting, whether in VBscript or any other tongue, but I've got something that does the job.  If you can use it - or better yet, modify it for your needs - have at it:

' scanfix.vbs
' 01/28/2014 chiclassiccomp.org
' Takes two sets of image files scanned as front and back pages and arranges them in order for import into Acrobat
' Assume that the back pages were scanned in reverse order (flipping over the stack without re-ordering)
' Instructions: create three directories "odd," "even" and "final."  Put odd (front) and even (back) pages in appropriate directory.
' Output is copied to "final."  Files are assumed to be using the TIF extension.  Change the two "oname = " lines if you're using a different extension.

Set objFSO = CreateObject("Scripting.FileSystemObject")
OddFolder = "odd"
EvenFolder = "even"
FinalFolder = "final"
ocount = 0
ecount = 0

' Count odd files
Set objFolder = objFSO.GetFolder(OddFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
    ocount=ocount+1
Next
wscript.Echo "There are " & ocount & " odd files"

' Count even files
Set objFolder = objFSO.GetFolder(EvenFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
    ecount=ecount+1
Next
wscript.Echo "There are " & ecount & " even files"

' Make sure they match
if ocount = ecount then
    wscript.Echo "File counts match.  Proceeding with copy and rename..."
else
    wscript.Echo "File counts don't match!"
    wscript.quit
end if

' Copy odd files to final dir and rename them properly
set objFolder = objFSO.GetFolder(OddFolder)
set colFiles = objFolder.Files
set filesys=CreateObject("Scripting.FilesystemObject")
fcount=1
For Each objFile in colFiles
    oname = "final\final" & fcount & ".tif"
    filesys.copyfile objfile,oname
'    wscript.echo objfile & " " & oname
    fcount = fcount + 2
Next

' Copy even files to final dir and rename them properly
set objFolder = objFSO.GetFolder(EvenFolder)
set colFiles = objFolder.Files
set filesys=CreateObject("Scripting.FilesystemObject")
fcount=ocount*2
For Each objFile in colFiles
    oname = "final\final" & fcount & ".tif"
    filesys.copyfile objfile,oname
'    wscript.echo objfile & " " & oname
    fcount = fcount - 2
Next


* Some things I (or you!) may want to add:
   - Padding the final filenames with leading zeroes to ensure proper ordering (so file10 doesn't come before file2, etc.)
   - Checking for the odd/even/final directories and making them if they're not there.  There's no reason for the user to have to do this.

No comments: