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

  • Question

  • 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

    • Moved by Bill_Stewart Tuesday, March 25, 2014 8:05 PM Abandoned
    Friday, January 31, 2014 4:28 PM

All replies

  • 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 .

    You should reverse the order of your actions:

    1. Monitor a folder for new files to be created.
    2. Move the files to another folder .
    3. Print the files that have the archive attribute set.
    4. Reset the archive attribute after printing.

    • Proposed as answer by jrv Friday, January 31, 2014 8:23 PM
    Friday, January 31, 2014 7:04 PM