Solved

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

Posted on 2009-04-09
7
880 Views
Last Modified: 2013-11-10
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
0
Comment
Question by:Craig_Campbell
  • 4
  • 3
7 Comments
 
LVL 30

Expert Comment

by:Scott Helmers
Comment Utility
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.

Scott

BTW, I adapted this from several scripts I found at Hey Scripting Guy
   http://www.microsoft.com/technet/scriptcenter/resources/qanda/hsgarch.mspx
In particular, the page on files and folders
   http://www.microsoft.com/technet/scriptcenter/resources/qanda/files.mspx
had this sample
   http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb07/hey0214.mspx

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""'")
 

Do

    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)
 

    ' INSERT PRINT COMMAND HERE
 

    objfile.delete
 

    ' this is dumb but useful method for stopping script during testing

    if strfilename = "C:\temp\stop.jpg" then wscript.quit

Loop

Open in new window

0
 

Author Comment

by:Craig_Campbell
Comment Utility
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.
script-conf.jpg
0
 
LVL 30

Expert Comment

by:Scott Helmers
Comment Utility
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.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 30

Expert Comment

by:Scott Helmers
Comment Utility
Craig,

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
   "C:\windows\system32"
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

Next

wscript.echo strVerbList

Open in new window

0
 

Author Comment

by:Craig_Campbell
Comment Utility
Thanks Scott,

Here is the screenshot you requested.

Cheers,

Craig
Screenshot2.JPG
0
 
LVL 30

Accepted Solution

by:
Scott Helmers earned 500 total points
Comment Utility
Craig,

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

Monitor-for-jpg-print-delete-v6.txt
0
 

Author Comment

by:Craig_Campbell
Comment Utility
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,

Craig
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Show hidden user account 7 25
Auto-Enrollment Group Policy 2 36
User profile Size Report 3 35
Path to Python 9 36
A short article about a problem I had getting the GPS LocationListener working.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

12 Experts available now in Live!

Get 1:1 Help Now