PDA

View Full Version : Solved: Batch file to clear print jobs



frank_m
10-28-2010, 09:42 PM
I have a batch file that stops and re-starts the spooling service and removes print jobs from the spool\printers folder

With Windows Vista Home version, it can be run by right clicking and selecting run as administrator.

Is there any way to have the batch file automatically call up the permission to run popup?

Below is a partial snippet of a vbscript that can call up the prompt:
do while x="RunFileasAnmin"
If WScript.Arguments.Count = 0 Then
Set ObjShell = CreateObject("Shell.Application")
ObjShellthree.ShellExecute "wscript.exe", """" & _
WScript.ScriptFullName & """" & " RunAsAdmin", , "runas", 1
x="skipblockabove"
Set ObjShell = Nothing
End if
loop So I was hoping that something similar could be accomplished with a batch file. The reason I need it is because the batch file will be called using vba in Excel.

The batch file code is;
@echo off
cls
net stop spooler
del %systemroot%\system32\spool\printers\*.shd
del %systemroot%\system32\spool\printers\*.spl
net start spooler If not, then I guess I'll have to work at doing the procedure with vbscript.

I posted a related question in the Excel help section of this forum: http://www.vbaexpress.com/forum/showthread.php?p=228563#post228563

Thanks in advance

frank_m
10-29-2010, 01:53 AM
I got it worked out.

Created a shortcut to the batch file. Renamed the shortcut "ClearPrintJobs" without the quotes and did not include the extension .lnk into the name.
Then I Right clicked on the shortcut, selected properties > the advanced button > checked the Run as Administrator checkbox,(Note: Only a short cut has the run as Administrator check box, if you check properties for a batch file there is no such option)
- Then with the code authored by Hermanito at http://www.mrexcel.com/forum/showthread.php?p=1498822
I substituted in my shortcut name including the .lnk extension and changed the vbhide Shell Parameter to 1, as to avoid the Administrator prompt from being minimized to the task bar. - It works :thumb
Sub RunClearPrintJobsBatchFileShortcut()

Dim FileName As String
FileName = Environ("USERPROFILE") & "\Desktop\" & "ClearPrintJobs.lnk"
FileName = "cmd /c " & """" & FileName & """"
Call Shell(FileName, 1)


End Sub