【问题标题】:Batch script: I want to run a batch script after giving the print command批处理脚本:我想在给出打印命令后运行一个批处理脚本
【发布时间】:2015-11-01 11:20:48
【问题描述】:

我希望在任何应用程序发出打印命令 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

【问题讨论】:

  • 告诉我们你尝试了什么。这里没有免费的代码编写服务!
  • 我有一个脚本可以清除打印队列,我想在给出打印命令 @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 > nul987654322@ 10 秒后运行该脚本
  • 从任何应用程序打印后运行它会非常困难(可能是不可能的)。你能告诉我们你想要完成什么吗?也许还有另一种方法可以实现。

标签: batch-file printing scripting windows-scripting


【解决方案1】:
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

【讨论】:

  • 大多数时候打印队列无法清除。这会导致随机或重新打印相同的文件。我想确保队列在持续时间之后变得清晰,以便用户可以手动进行下一次打印。所以账单如何使用这个脚本
【解决方案2】:

我在 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

【讨论】:

    猜你喜欢
    • 2011-06-15
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    相关资源
    最近更新 更多