Wednesday, May 02, 2012

How to Print large number of files automatically

Despite being in the second decade of the 21st. century when bits have replaced pages we still have to occasionally print out large numbers of documents when dealing with banks, government departments and the like.  I recently had the need to take printouts of a few dozen documents of assorted types - Word documents, pdf files, jpg images, tif files etc.  Doing this manually would have been quite painful and would involve
  • opening the file by double-clicking on it
  • choosing print
  • clicking on print
And I would have to repeat these steps for every file.  Instead I wrote the following Powershell  function to automate this:

function Print-Docs 
{
    Begin { $shell = New-object -comobject Shell.Application }
    process {
        $parent = (Split-Path (Get-Item $_))
        $file = (Split-Path -Leaf (Get-Item $_))
        
        if($parent -and $file) {
            $folder = $shell.NameSpace($parent)
            Write-Debug ("Printing file '{0}' from folder '{1}'`n" -f $file, $parent)
            $file = $folder.ParseName($file)
            $file.InvokeVerb("Print")
        }
        else {
            Write-Warning "Missing file $_"
        }
    }
}

After this it was a simple matter to do something like

PS: 64 > ls *.xlsx,*.docs,*.jpg | Print-Docs
and  I had my documents printed out thus saving me at least fifteen minutes.

Note - this will work only on Windows 7 and newer versions of Windows (unless you installed Powershell on an older version). 

Food for thought:
  • Some of the programs (e.g. the jpg program) prompts the user for some input.  How can this be modified so that the program is truly automated?
  • How would you do this in a UNIX environment (I remember vaguely that you could do automation of GUI programs through wish and TK)?

 

No comments: