(view source code of printing.bat as plain text)
@ECHO OFF
:: Check Windows version
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
SETLOCAL ENABLEDELAYEDEXPANSION
:: Check number of command line arguments
IF "%~1"=="" GOTO Syntax
IF NOT "%~4"=="" GOTO Syntax
:: Check if WMIC is available
WMIC.EXE /? >NUL 2>&1 || GOTO Syntax
:: Initialize variables
SET Printer=%~1
SET PrinterSet=0
SET UseAllPrn=0
SET UseDefault=0
SET Action=%~2
SET ActFlush=0
SET ActList=0
SET ActPause=0
SET ActResume=0
SET ActionSet=0
SET Option=%~3
SET OptionSet=0
SET Quiet=0
:: Parse command line arguments
:: Mandatory first argument: printer name, /All, /Default or /List
IF NOT "%Printer%"=="" (
IF /I "%Printer:~0,2%"=="/L" (
IF NOT "%~2"=="" GOTO Syntax
SET Action=List
SET ActList=1
SET ActionSet=1
SET Printer=/All
SET UseAllPrn=1
SET PrinterSet=1
)
IF /I "!Printer:~0,2!"=="/A" (
SET UseAllPrn=1
SET PrinterSet=1
)
IF /I "!Printer:~0,2!"=="/D" (
SET UseDefault=1
SET PrinterSet=1
)
ECHO "!Printer!" | FINDSTR /R /C:"[/%%?\*]" >NUL
IF ERRORLEVEL 1 SET PrinterSet=1
)
IF NOT "%PrinterSet%"=="1" GOTO Syntax
:: Second argument, mandatory unless the first argument is /List: /Flush, /List, /Pause or /Resume
IF /I "%Action:~0,2%"=="/L" GOTO Syntax
IF /I "%Action:~0,2%"=="/F" (
SET Action=CancelAllJobs
SET ActFlush=1
SET ActionSet=1
)
IF /I "%Action:~0,2%"=="/P" (
SET Action=Pause
SET ActPause=1
SET ActionSet=1
)
IF /I "%Action:~0,2%"=="/R" (
SET Action=Resume
SET ActResume=1
SET ActionSet=1
)
IF NOT "%ActionSet%"=="1" GOTO Syntax
:: Optional third argument: /Quiet or /Verbose (default)
IF "%Option%"=="" (
SET OptionSet=1
) ELSE (
IF /I "%Option:~0,2%"=="/Q" (
SET Quiet=1
SET OptionSet=1
)
IF /I "%Option:~0,2%"=="/V" (
SET OptionSet=1
)
)
IF NOT "%OptionSet%"=="1" GOTO Syntax
SET Query1=Path Win32_Printer
IF "%UseDefault%"=="1" (
SET Query1=%Query1% WHERE "Default='TRUE'"
) ELSE (
IF NOT "%UseAllPrn%"=="1" (
SET Query1=%Query1% WHERE "DeviceID='%Printer%'"
)
)
:: Status number to descriptive text
SET Status_0=--Error--
SET Status_1=Other
SET Status_2=Unknown
SET Status_3=Idle
SET Status_4=Printing
SET Status_5=Warmup
SET Status_6=StoppedPrinting
SET Status_7=Offline
SET Status_8=Paused
SET Status_9=Error
SET Status_10=Busy
SET Status_11=NotAvailable
SET Status_12=Waiting
SET Status_13=Processing
SET Status_14=Initialization
SET Status_15=PowerSave
SET Status_16=PendingDeletion
SET Status_17=IOActive
SET Status_18=ManualFeed
IF "%ActFlush%"=="1" (
FOR /F "tokens=1* delims==" %%A IN ('WMIC %Query1% Get DeviceID /Format:list ^| FIND "="') DO CALL :FlushJobs "%%~B"
) ELSE (
FOR /F "tokens=1* delims==" %%A IN ('WMIC %Query1% Get DeviceID /Format:list ^| FIND "="') DO CALL :PRPrinting "%%~B"
)
GOTO:EOF
ENDLOCAL
:FlushJobs
SETLOCAL
:: Query the printer
FOR /F "tokens=*" %%A IN ('WMIC Path Win32_Printer WHERE "DeviceID='%~1'" Get DeviceID^,ExtendedPrinterStatus /Format:list ^| FIND "="') DO SET %%A
:: Get number of printjobs
SET PrintJobs=0
FOR /F "tokens=*" %%A IN ('WMIC Path Win32_PrintJob WHERE "Name LIKE '%~1%%'" Get Name /Format:list 2^>^&1 ^| FIND "="') DO SET /A PrintJobs += 1
:: Display the results
ECHO Printer : %DeviceID%
ECHO Print Jobs : %PrintJobs%
ECHO Status : !Status_%ExtendedPrinterStatus%!
IF %PrintJobs% GTR 0 (
ECHO Flush all printjobs . . .
WMIC Path Win32_Printer WHERE "DeviceID='%~1'" Call %Action% >NUL 2>&1
:: Display the result
SET PrintJobs=0
FOR /F "tokens=*" %%A IN ('WMIC Path Win32_PrintJob WHERE "Name LIKE '%~1%%'" Get Name /Format:list 2^>^&1 ^| FIND "="') DO SET /A PrintJobs += 1
ECHO Print Jobs : %PrintJobs%
)
ECHO.
ENDLOCAL
GOTO:EOF
:PRPrinting
SETLOCAL
:: Query the printer
FOR /F "tokens=*" %%A IN ('WMIC Path Win32_Printer WHERE "DeviceID='%~1'" Get DeviceID^,ExtendedPrinterStatus /Format:list ^| FIND "="') DO SET %%A
:: Get number of printjobs
SET PrintJobs=0
FOR /F "tokens=*" %%A IN ('WMIC Path Win32_PrintJob WHERE "Name LIKE '%~1%%'" Get Name /Format:list 2^>^&1 ^| FIND "="') DO SET /A PrintJobs += 1
:: Display the results
ECHO Printer : %DeviceID%
ECHO Print Jobs : %PrintJobs%
ECHO Status : !Status_%ExtendedPrinterStatus%!
:: Resume if paused
IF %ActResume% EQU 1 IF %ExtendedPrinterStatus% EQU 8 (
ECHO %Action% printing . . .
WMIC Path Win32_Printer WHERE "DeviceID='%~1'" Call %Action% >NUL 2>&1
FOR /F "tokens=*" %%A IN ('WMIC Path Win32_Printer WHERE "DeviceID='%~1'" Get ExtendedPrinterStatus /Format:list ^| FIND "="') DO SET New%%A
)
:: Pause if not yet paused
IF %ActPause% EQU 1 IF %ExtendedPrinterStatus% NEQ 8 (
ECHO %Action% printing . . .
WMIC Path Win32_Printer WHERE "DeviceID='%~1'" Call %Action% >NUL 2>&1
FOR /F "tokens=*" %%A IN ('WMIC Path Win32_Printer WHERE "DeviceID='%~1'" Get ExtendedPrinterStatus /Format:list ^| FIND "="') DO SET New%%A
)
:: Display new status
IF %ActResume% EQU 1 IF %ExtendedPrinterStatus% EQU 8 (
ECHO Status : !Status_%NewExtendedPrinterStatus%!
)
IF %ActPause% EQU 1 IF %ExtendedPrinterStatus% NEQ 8 (
ECHO Status : !Status_%NewExtendedPrinterStatus%!
)
ECHO.
ENDLOCAL
GOTO:EOF
:Syntax
ECHO.
ECHO Printing.bat, Version 2.20 for Windows XP or later
ECHO Pause or resume printing, or flush all queued printjobs on the specified
ECHO printer(s), or list all printers, their status and number of printjobs
ECHO.
ECHO Usage: PRINTING printer action [ option ]
ECHO.
ECHO or: PRINTING /List
ECHO.
ECHO Where: "printer" is either /All, /Default or a printer name
ECHO "action" is either /Pause, /Resume or /Flush
ECHO "option" is either /Quiet or /Verbose (default)
ECHO.
ECHO Notes: Use doublequotes if the printer name contains spaces.
ECHO Do not specify a printer when /List switch is used.
ECHO Switches may be abbreviated, e.g. /D instead of /Default.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
IF "%OS%"=="Windows_NT" ENDLOCAL
IF "%OS%"=="Windows_NT" EXIT /B 1
page last modified: 2024-04-16; loaded in 0.0067 seconds