Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for printing.bat

(view source code of printing.bat as plain text)

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. SETLOCAL ENABLEDELAYEDEXPANSION
  6.  
  7. :: Check number of command line arguments
  8. IF     "%~1"=="" GOTO Syntax
  9. IF NOT "%~4"=="" GOTO Syntax
  10.  
  11. :: Check if WMIC is available
  12. WMIC.EXE /? >NUL 2>&1 || GOTO Syntax
  13.  
  14. :: Initialize variables
  15. SET Printer=%~1
  16. SET PrinterSet=0
  17. SET UseAllPrn=0
  18. SET UseDefault=0
  19. SET Action=%~2
  20. SET ActFlush=0
  21. SET ActList=0
  22. SET ActPause=0
  23. SET ActResume=0
  24. SET ActionSet=0
  25. SET Option=%~3
  26. SET OptionSet=0
  27. SET Quiet=0
  28.  
  29. :: Parse command line arguments
  30. :: Mandatory first argument: printer name, /All, /Default or /List
  31. IF NOT "%Printer%"=="" (
  32. 	IF /I "%Printer:~0,2%"=="/L" (
  33. 		IF NOT "%~2"=="" GOTO Syntax
  34. 		SET Action=List
  35. 		SET ActList=1
  36. 		SET ActionSet=1
  37. 		SET Printer=/All
  38. 		SET UseAllPrn=1
  39. 		SET PrinterSet=1
  40. 	)
  41. 	IF /I "!Printer:~0,2!"=="/A" (
  42. 		SET UseAllPrn=1
  43. 		SET PrinterSet=1
  44. 	)
  45. 	IF /I "!Printer:~0,2!"=="/D" (
  46. 		SET UseDefault=1
  47. 		SET PrinterSet=1
  48. 	)
  49. 	ECHO "!Printer!" | FINDSTR /R /C:"[/%%?\*]" >NUL
  50. 	IF ERRORLEVEL 1 SET PrinterSet=1
  51. )
  52. IF NOT "%PrinterSet%"=="1" GOTO Syntax
  53.  
  54. :: Second argument, mandatory unless the first argument is /List: /Flush, /List, /Pause or /Resume
  55. IF /I "%Action:~0,2%"=="/L" GOTO Syntax
  56. IF /I "%Action:~0,2%"=="/F" (
  57. 	SET Action=CancelAllJobs
  58. 	SET ActFlush=1
  59. 	SET ActionSet=1
  60. )
  61. IF /I "%Action:~0,2%"=="/P" (
  62. 	SET Action=Pause
  63. 	SET ActPause=1
  64. 	SET ActionSet=1
  65. )
  66. IF /I "%Action:~0,2%"=="/R" (
  67. 	SET Action=Resume
  68. 	SET ActResume=1
  69. 	SET ActionSet=1
  70. )
  71. IF NOT "%ActionSet%"=="1" GOTO Syntax
  72.  
  73. :: Optional third argument: /Quiet or /Verbose (default)
  74. IF "%Option%"=="" (
  75. 	SET OptionSet=1
  76. ) ELSE (
  77. 	IF /I "%Option:~0,2%"=="/Q" (
  78. 		SET Quiet=1
  79. 		SET OptionSet=1
  80. 	)
  81. 	IF /I "%Option:~0,2%"=="/V" (
  82. 		SET OptionSet=1
  83. 	)
  84. )
  85. IF NOT  "%OptionSet%"=="1" GOTO Syntax
  86.  
  87. SET Query1=Path Win32_Printer
  88. IF "%UseDefault%"=="1" (
  89. 	SET Query1=%Query1% WHERE "Default='TRUE'"
  90. ) ELSE (
  91. 	IF NOT "%UseAllPrn%"=="1" (
  92. 		SET Query1=%Query1% WHERE "DeviceID='%Printer%'"
  93. 	)
  94. )
  95.  
  96. :: Status number to descriptive text
  97. SET Status_0=--Error--
  98. SET Status_1=Other
  99. SET Status_2=Unknown
  100. SET Status_3=Idle
  101. SET Status_4=Printing
  102. SET Status_5=Warmup
  103. SET Status_6=StoppedPrinting
  104. SET Status_7=Offline
  105. SET Status_8=Paused
  106. SET Status_9=Error
  107. SET Status_10=Busy
  108. SET Status_11=NotAvailable
  109. SET Status_12=Waiting
  110. SET Status_13=Processing
  111. SET Status_14=Initialization
  112. SET Status_15=PowerSave
  113. SET Status_16=PendingDeletion
  114. SET Status_17=IOActive
  115. SET Status_18=ManualFeed
  116.  
  117. IF "%ActFlush%"=="1" (
  118. 	FOR /F "tokens=1* delims==" %%A IN ('WMIC %Query1% Get DeviceID /Format:list ^| FIND "="') DO CALL :FlushJobs "%%~B"
  119. ) ELSE (
  120. 	FOR /F "tokens=1* delims==" %%A IN ('WMIC %Query1% Get DeviceID /Format:list ^| FIND "="') DO CALL :PRPrinting "%%~B"
  121. )
  122.  
  123. GOTO:EOF
  124. ENDLOCAL
  125.  
  126.  
  127.  
  128.  
  129. :FlushJobs
  130. SETLOCAL
  131. :: Query the printer
  132. FOR /F "tokens=*" %%A IN ('WMIC Path Win32_Printer  WHERE "DeviceID='%~1'" Get DeviceID^,ExtendedPrinterStatus /Format:list ^| FIND "="') DO SET %%A
  133.  
  134. :: Get number of printjobs
  135. SET PrintJobs=0
  136. 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
  137.  
  138. :: Display the results
  139. ECHO Printer    : %DeviceID%
  140. ECHO Print Jobs : %PrintJobs%
  141. ECHO Status     : !Status_%ExtendedPrinterStatus%!
  142.  
  143. IF %PrintJobs% GTR 0 (
  144. 	ECHO Flush all printjobs . . .
  145. 	WMIC Path Win32_Printer WHERE "DeviceID='%~1'" Call %Action% >NUL 2>&1
  146.  
  147. 	:: Display the result
  148. 	SET PrintJobs=0
  149. 	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
  150. 	ECHO Print Jobs : %PrintJobs%
  151. )
  152. ECHO.
  153. ENDLOCAL
  154. GOTO:EOF
  155.  
  156.  
  157.  
  158.  
  159. :PRPrinting
  160. SETLOCAL
  161. :: Query the printer
  162. FOR /F "tokens=*" %%A IN ('WMIC Path Win32_Printer  WHERE "DeviceID='%~1'" Get DeviceID^,ExtendedPrinterStatus /Format:list ^| FIND "="') DO SET %%A
  163.  
  164. :: Get number of printjobs
  165. SET PrintJobs=0
  166. 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
  167.  
  168. :: Display the results
  169. ECHO Printer    : %DeviceID%
  170. ECHO Print Jobs : %PrintJobs%
  171. ECHO Status     : !Status_%ExtendedPrinterStatus%!
  172.  
  173. :: Resume if paused
  174. IF %ActResume% EQU 1 IF %ExtendedPrinterStatus% EQU 8 (
  175. 	ECHO %Action% printing . . .
  176. 	WMIC Path Win32_Printer WHERE "DeviceID='%~1'" Call %Action% >NUL 2>&1
  177. 	FOR /F "tokens=*" %%A IN ('WMIC Path Win32_Printer WHERE "DeviceID='%~1'" Get ExtendedPrinterStatus /Format:list ^| FIND "="') DO SET New%%A
  178. )
  179.  
  180. :: Pause if not yet paused
  181. IF %ActPause% EQU 1 IF %ExtendedPrinterStatus% NEQ 8 (
  182. 	ECHO %Action% printing . . .
  183. 	WMIC Path Win32_Printer WHERE "DeviceID='%~1'" Call %Action% >NUL 2>&1
  184. 	FOR /F "tokens=*" %%A IN ('WMIC Path Win32_Printer WHERE "DeviceID='%~1'" Get ExtendedPrinterStatus /Format:list ^| FIND "="') DO SET New%%A
  185. )
  186.  
  187. :: Display new status
  188. IF %ActResume% EQU 1 IF %ExtendedPrinterStatus% EQU 8 (
  189. 	ECHO Status     : !Status_%NewExtendedPrinterStatus%!
  190. )
  191. IF %ActPause% EQU 1 IF %ExtendedPrinterStatus% NEQ 8 (
  192. 	ECHO Status     : !Status_%NewExtendedPrinterStatus%!
  193. )
  194. ECHO.
  195. ENDLOCAL
  196. GOTO:EOF
  197.  
  198.  
  199.  
  200.  
  201. :Syntax
  202. ECHO.
  203. ECHO Printing.bat, Version 2.20 for Windows XP or later
  204. ECHO Pause or resume printing, or flush all queued printjobs on the specified
  205. ECHO printer(s), or list all printers, their status and number of printjobs
  206. ECHO.
  207. ECHO Usage:  PRINTING   printer   action   [ option ]
  208. ECHO.
  209. ECHO    or:  PRINTING   /List
  210. ECHO.
  211. ECHO Where:  "printer"  is either /All, /Default or a printer name
  212. ECHO         "action"   is either /Pause, /Resume or /Flush
  213. ECHO         "option"   is either /Quiet or /Verbose (default)
  214. ECHO.
  215. ECHO Notes:  Use doublequotes if the printer name contains spaces.
  216. ECHO         Do not specify a printer when /List switch is used.
  217. ECHO         Switches may be abbreviated, e.g. /D instead of /Default.
  218. ECHO.
  219. ECHO Written by Rob van der Woude
  220. ECHO http://www.robvanderwoude.com
  221.  
  222. IF "%OS%"=="Windows_NT" ENDLOCAL
  223. IF "%OS%"=="Windows_NT" EXIT /B 1
  224.  

page last modified: 2024-04-16; loaded in 0.0067 seconds