批处理脚本:我想在给出print命令后运行批处理脚本

时间:2015-08-09 12:48:53

标签: batch-file printing scripting windows-scripting

我希望在从任何应用程序提供打印命令10秒后执行脚本。

@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

2 个答案:

答案 0 :(得分: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

改编自http://www.codeproject.com/Articles/42212/WMI-and-File-System-Monitoring

这也启动了服务

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

这会删除打印机文件夹中的文件

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

答案 1 :(得分:0)

我在vwork,我的电脑在家里破了。这样的事情。

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