Poll a network folder for new files/ images and automatically print them to a specific printer

Hi all,

I need a script or (preferably free) program that will meet the following requirements:

1) Poll a network folder for new JPEG image files (for example, every 5 minutes)
2) If there is a new file, print it to a network printer
3) Once printed, delete the file

Does anyone know of anything, or direct me to something that will do this?
It will be run in a Windows environment
Who is Participating?
Scott HelmersVisio Consultant, Trainer, Author, and DeveloperCommented:

Progress! Armed with the print command from your dialog and what I found yesterday, the script is closer to the end objective. I think there are two deficiencies at the moment but both can be overcome.

On my system, the jpg print command launches the Windows picture manager, which requires user intervention to complete the printing. I assume one of two things is true on your system: either a different program is associated with printing jpgs and it runs unattended, or you print in a different way. For example, if you print from the command line, let me know the exact syntax that you use and we should be able to replicate it in the script.

The second deficiency is related to timing and is actually a secondary effect of the first problem described above -- because manual intervention takes time, i had to insert an artificial sleep timer (currently set for 20 seconds). This allows me to complete the printing before the script tries to delete the file. Without this delay, the file is gone before I can print it!

The easiest way to solve both problems is probably to print from the command line. I can do that in a way that pauses the script until the printing event completes, which, I think, will do the trick.

One other note: you can now set the name of the folder you want to monitor and the time intercal for monitoring. Just look for the comments that say SET FOLDER NAME HERE and SET MONITOR INTERVAL HERE.

I just realized I lied above -- there is a third deficiency: the script prints to the default printer. It will take some more work to get it to print to a network printer. You might consider asking that as a related question because it will take some investigation, or perhaps one of the other experts will know that answer.


Scott HelmersVisio Consultant, Trainer, Author, and DeveloperCommented:
I think I've got everything here you need -- except for the printing. Refer to comments in the script for information about how it works and where you'll need to insert the print command. I'll try to get back to this later to figure out the printing part (I need to do some real work now!) but if someone else can contribute that part, that would be great.

Note that you can change the time interval for how often the script checks for new files.


BTW, I adapted this from several scripts I found at Hey Scripting Guy
In particular, the page on files and folders
had this sample

wscript.echo "starting now"
Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
' statement below creates event that monitors a folder (C:\test) for new files every 60 seconds
' change number here: "WITHIN 60" to change timer from 60 seconds to another value
' specifying directory name is weird: you must type four backslashes for each backslash
' you need in path;
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceCreationEvent WITHIN 60 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""c:\\\\test""'")
    Set objLatestEvent = colMonitoredEvents.NextEvent
    strNewFile = objLatestEvent.TargetInstance.PartComponent
    arrNewFile = Split(strNewFile, "=")
    strFileName = arrNewFile(1)
    strFileName = Replace(strFileName, "\\", "\")
    strFileName = Replace(strFileName, Chr(34), "")
    ' comment this out when everything is working
    wscript.echo "filename: " & strfilename
    set objFile = objFSO.GetFile(strFilename)
    ' this is dumb but useful method for stopping script during testing
    if strfilename = "C:\temp\stop.jpg" then wscript.quit

Open in new window

Craig_CampbellAuthor Commented:
Thanks Scott,

That is looking great so far. Thanks for your time and effort, it is greatly appreciated.

One question, whenever I run the script a popup box comes up with the filename of the new file added to the folder (I have attached a jpeg of the popup). Is there a way to make this silent and automatically select OK on the confirmation box or something?

Also, if you could help with the print function that would be great.

Thanks again for your help mate.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Scott HelmersVisio Consultant, Trainer, Author, and DeveloperCommented:
I just put that dialog in for testing purposes; look for this line
   wscript.echo "filename: " & strfilename
and either delete it or put an apostrophe (single quote) in front of it to comment it out.

You timing is good -- I just sat down to see whether I can solve the printing problem. No promises for tonight but I'll see what I can find.
Scott HelmersVisio Consultant, Trainer, Author, and DeveloperCommented:

I may have stumbled onto the right path...

The script below will list all of the verbs we can apply to a jpg file; one of them should allow us to print it (it says here in fine print).

In the script, change
to the path containing the folder you want to monitor.

Put a jpg file into that folder then change "notepad.exe" to the name of the file.

Run the script and post a screen shot of the results here.

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace("C:\windows\system32")
Set objFolderItem = objFolder.ParseName("notepad.exe")
Set colVerbs = objFolderItem.Verbs
For Each objVerb in colVerbs
    strVerbList = strVerbList & objVerb & vbCRLF
wscript.echo strVerbList

Open in new window

Craig_CampbellAuthor Commented:
Thanks Scott,

Here is the screenshot you requested.


Craig_CampbellAuthor Commented:
Sorry Scott,

Only just got back online mate.Yeah, everything works great except for the Printing Wizard requiring user intervention. I have tried a few different programs and the command line (net use, net rint etc) but with no success so far. I wonder if there is a silent switch at the command line for any third party programs? Will have to investigate this and get back to you.

Cheers mate,

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.