Scheduled process to print and delete

Posted on 2014-07-16
Last Modified: 2014-07-22

Is there a way to monitor the contents of a folder (PDF files) on a scheduled task to print to a printer and delete (the file) after successful print? The line of business will dump PDF reports into a folder - I want a scheduled task to monitor this folder, print, and delete.


Question by:appliedimg
    LVL 8

    Assisted Solution


    This is straight forward enough to achieve using VBScript & Windows Scheduled Tasks.

    This code will process files within a target folder, if the file is a PDF then it will, using the Wscript Shell object run the Acrobat Reader executable with some command line parameters.

    It will print the documents to the default printer on the machine.
    On Error Resume Next
    ' Set a String value to the path of the folder to be processed
    FolderToMonitor = "C:\MyDev"
    ' Set the File System Object so we can access Folders and Files
    Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
    ' Get the Folder to be processed so we can use the item within it
    Set Folder = FileSystemObject.GetFolder(FolderToMonitor)
    ' Get all the files currently in the folder
    Set Files = Folder.Files
    ' Loop throuhg the list of files
    For Each File In Files
    	' Get the File Name
    	FileName = File.Name
    	' Check that the file is a PDF
    	If Right(UCase(FileName), 3) = "PDF" Then
    		' Clear any error codes
    		' Set and use the Wscript Shell object to run a command
    		Set WscriptShell = CreateObject ("Wscript.Shell") """C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe"" /p /h" & FileName,,True
    		' If the Err Number returned is 0 (Success) then delete the file
    		If Err.Number = 0 Then
    			FileSystemObject.DeleteFile FileName
    		End If
    	End If
    ' Move on to the next file	

    Open in new window


    Author Comment

    jawa29 - thanks much for your response.

    I used your script but unfortunately it neither prints not deletes the files. I've added to display msgbox each step of the script and it hits all the loops and captures file name etc. The Err.Number value returned is also 0, however, nothing prints not deletes. Any thoughts?

    Also, is it possible to specify the printer as well?

    Author Comment

    Also, I do get "There was an error opening this document. This file cannot be found." error message when I run the script and it launches Adobe Reader.Capture.JPG

    Accepted Solution

    I've used this script instead:

    strComputer = "."
    TargetFolder = "C:\Files\ToPrint\" 
    Set objShell = CreateObject("Shell.Application") 
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objShell.Namespace(TargetFolder) 
    Set colItems = objFolder.Items 
    For Each objItem In colItems 
    	FileName = objItem.Name
    	objItem.InvokeVerbEx ("Print") 
    	objFSO.DeleteFile(TargetFolder & FileName), True

    Open in new window

    LVL 8

    Expert Comment

    Hi there,

    I'm glad you found a solution, I will look at why my script didn't work for my own curiosity.

    Author Closing Comment

    My answer is what I was looking for. jawa29 did help with ideas for me to get there.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
    This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now