-2

I want a script to be executed after 10 seconds of giving the print command from any application.

@echo off
echo.
echo Purging the print queue...
net stop Spooler
echo Deleting all print jobs...
ping localhost -n 4 > nul
del /q %SystemRoot%\system32\spool\printers\*.*
net start Spooler
echo Done!
ping localhost -n 4 > nul
aphoria
  • 18,540
  • 7
  • 58
  • 69
  • 1
    Show us what have you tried. No free-code writing service available here! – Swastik Padhi Aug 09 '15 at 12:50
  • I have a script that clears the print queue , whcih i want to run after 10 seconds of giving the print command `@echo off echo. echo Purging the print queue... net stop Spooler echo Deleting all print jobs... ping localhost -n 4 > nul del /q %SystemRoot%\system32\spool\printers\*.* net start Spooler echo Done! ping localhost -n 4 > nul` – Sameer Shah Aug 09 '15 at 12:52
  • It's going to be very difficult (probably impossible) to run this after printing from any application. Can you tell us what you are trying to accomplish? Maybe there is another way to achieve it. – aphoria Aug 09 '15 at 13:38

2 Answers2

0
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & _
    "." & "\root\cimv2")


Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("Select * From __InstanceCreationEvent Within 5 Where " _
    & "Targetinstance Isa 'CIM_DirectoryContainsFile' and " _
    & "TargetInstance.GroupComponent= " _
    & "'Win32_Directory.Name=""c:\\\\Windows\\\\System32\\\\Spool\\\\Printers""'")

Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop

Adapted from http://www.codeproject.com/Articles/42212/WMI-and-File-System-Monitoring

Also this starts a service

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * From Win32_Service")

For Each objItem in colItems
    If Lcase(objitem.Name) = "spooler" Then
        msgbox objitem.name & " " & objitem.status  & " " & objitem.state
        objitem.StartService
    End If
Next

And this deletes files in the printers folder

On error resume next
Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = fso.GetFolder("c:\windows\system32\spool\Printers")
For each f in fldr.files
    f.delete
Next
bill
  • 205
  • 1
  • 3
  • Most of the time the print queue fails to clear. This causes random or reprinting of the same files. I want to assure that the queue gets clear after the time duration so that the user can manually give teh next print. So bill how to use this script – Sameer Shah Aug 10 '15 at 12:43
0

I'm at vwork and my computer broke at home. Something like this.

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & _
    "." & "\root\cimv2")


Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("Select * From __InstanceCreationEvent Within 5 Where " _
    & "Targetinstance Isa 'CIM_DirectoryContainsFile' and " _
    & "TargetInstance.GroupComponent= " _
    & "'Win32_Directory.Name=""c:\\\\Windows\\\\System32\\\\Spool\\\\Printers""'")

Do
    wscript.scleep 1000
    On error resume next
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fldr = fso.GetFolder("c:\windows\system32\spool\Printers")
    For each f in fldr.files
        f.delete
    Next
Loop
billg
  • 1