Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Monitor a folder for new files, print them and then move them to a different folder using VB Script

$
0
0

I am new to vbscript, and I am building a script to complete the following actions.

1. Monitor a folder for new files to be created.

2. Print those files

3. Move the files to anothe folder .

Although this seems fairly simple, I am having trouble with the timing.  All of the files that this folder will contain are pdf files that can range from 1 page up to 60 pages.  I have a code that works but after it encounters the first pdf file it takes too long to open pdf that goes into the folder. Which means that it does not open in enough time before the script moves it to the new folder.  Are there any suggestions out there as to how to fix it.  I should mention that this folder could recieve several documents per minute.  This will be located on our fax server.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & _
        strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceCreationEvent WITHIN 20 WHERE " _
        & "Targetinstance Isa 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""C:\\\\test folder""'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent

'print the new files

TargetFolder = "C:\test folder" 
 
 Set objShell = CreateObject("Shell.Application")
 Set objFolder = objShell.Namespace(TargetFolder) 
 Set colItems = objFolder.Items
 For i = 0 to colItems.Count - 1
     colItems.Item(i).InvokeVerbEx("Print")
  Next

'close the AcroRd32.exe 

 strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'AcroRd32.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next

'pause before moving the files 

Set WshShell = WScript.CreateObject("WScript.Shell")
 wscript.sleep 10000
 
'Move the files after printing

 
Set fso=CreateObject("Scripting.FileSystemObject")
 Set fldr = fso.getFolder("C:\test folder")
 If fldr.files.Count > 0 then
 fso.MoveFile "C:\test folder\*", "C:\Documents and Settings\ddavis\Desktop\testCompleted"

 

 End If


Loop


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>