Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for printpdf.bat

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

  1. @ECHO OFF
  2. :: Check Windows version, abort if not NT 4 or later
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4. SETLOCAL ENABLEDELAYEDEXPANSION
  5.  
  6. :: Initialize variables
  7. SET PrintCmd=
  8. SET Temp=%Temp:"=%
  9. SET NumFiles=0
  10. SET MultiPrint=0
  11. SET ListTool=
  12.  
  13. :: Check command line arguments
  14. IF     "%~1"=="" GOTO Syntax
  15. IF NOT "%~3"=="" GOTO Syntax
  16. IF     "%~2"=="" (
  17. 	SET FileSpec=%~1
  18. ) ELSE (
  19. 	IF /I "%~1"=="/M" SET FileSpec=%~2
  20. 	IF /I "%~2"=="/M" SET FileSpec=%~1
  21. )
  22. ECHO.%* | FIND /I "/M" >NUL && SET MultiPrint=1
  23. ECHO.%FileSpec% | FIND "/" >NUL && GOTO Syntax
  24. IF NOT EXIST "%FileSpec%" GOTO Syntax
  25.  
  26. :: Count the number of files specified by filespec
  27. FOR %%A IN (%FileSpec%) DO SET /A NumFiles = !NumFiles! + 1
  28. IF %NumFiles% EQU 0 GOTO Syntax
  29.  
  30. :: Check if we need to have access to a list of processes
  31. :: currently running, and if so, which one is available
  32. IF %NumFiles%   GTR 1 SET  MultiPrint=1
  33. IF %MultiPrint% EQU 0 CALL :GetListTool
  34.  
  35. :: Get the file association from the registry
  36. FOR /F "tokens=1* delims==" %%A IN ('ASSOC .PDF') DO (
  37. 	FOR /F "tokens=1 delims==" %%C IN ('FTYPE ^| FIND /I "%%~B"') DO (
  38. 		CALL :GetPrintCommand %%C
  39. 	)
  40. )
  41.  
  42. :: Check if a print command was found
  43. IF NOT DEFINED PrintCmd (
  44. 	ECHO.
  45. 	ECHO No print command seems to be assiociated with .PDF files on this computer.
  46. 	GOTO Syntax
  47. )
  48.  
  49. :: Print the file using the print command we just found
  50. FOR %%A IN (%FileSpec%) DO CALL :ExecPrintCommand "%%~fA"
  51.  
  52. :: A final message
  53. IF "%MultiPrint%"=="1" (
  54. 	ECHO.
  55. 	ECHO You will need to close the Acrobat Reader window manually after the printing
  56. 	ECHO is finished.
  57. 	IF "%NumFiles%"=="1" IF "%ListTool%"=="" (
  58. 		ECHO To close that window automatically next time you print a single PDF file,
  59. 		ECHO download and install PSLIST from the Microsoft website:
  60. 		ECHO http://www.microsoft.com/technet/sysinternals/utilities/pstools.mspx
  61. 	)
  62. )
  63.  
  64. :: Done
  65. GOTO End
  66.  
  67.  
  68. :ExecPrintCommand
  69. CALL START /MIN "PrintPDF" %PrintCmd%
  70. GOTO:EOF
  71.  
  72.  
  73. :GetListTool
  74. :: Now we need to find a tool to check for processes.
  75. :: In XP and later this will be the native TASKLIST command,
  76. :: in NT 4 and 2000 we'll need to find a non-native tool.
  77. :: First we'll try TASKLIST ...
  78. TASKLIST >NUL 2>&1
  79. IF ERRORLEVEL 1 (
  80. 	REM ... if TASKLIST isn't available we'll try TLIST next ...
  81. 	TLIST >NUL 2>&1
  82. 	IF ERRORLEVEL 1 (
  83. 		REM ... and if that isn't available either we'll try PSLIST ...
  84. 		PSLIST >NUL 2>&1
  85. 		IF NOT ERRORLEVEL 1 SET ListTool=PSLIST
  86. 	) ELSE (
  87. 		SET ListTool=TLIST
  88. 	)
  89. ) ELSE (
  90. 	SET ListTool=TASKLIST
  91. )
  92. :: Don't worry if we didn't find ANY tool to list processes, in
  93. :: that case we'll just assume multiple PDFs need to be printed
  94. IF "%ListTool%"=="" SET MultiPrint=1
  95. GOTO:EOF
  96.  
  97.  
  98. :GetPrintCommand
  99. :: Get the print command for this file type from the registry
  100. START /WAIT REGEDIT.EXE /E "%Temp%.\pdf.dat" "HKEY_CLASSES_ROOT\%1\shell\print\command"
  101. IF NOT EXIST "%Temp%.\pdf.dat" GOTO:EOF
  102. FOR /F "tokens=1* delims==" %%D IN ('TYPE "%TEMP%.\pdf.dat" ^| FIND "@="') DO SET PrintCmd=%%E
  103. DEL "%Temp%.\pdf.dat"
  104. SET PrintCmd=%PrintCmd:\"="%
  105. SET PrintCmd=%PrintCmd:""="%
  106. SET PrintCmd=%PrintCmd:\\=\%
  107. :: The /T switch terminates Acrobat Reader after printing.
  108. :: Thanks to Fabio Quieti for sending me this tip.
  109. :: However, as Michael Butler pointed out, it should not be
  110. :: used when printing lots of files.
  111. :: So I introduced a /M switch for this batch file, stating
  112. :: that multiple files are to be printed.
  113. :: Without specifying the /M switch, this will also be true
  114. :: when wildcards are used in the filespec.
  115. :: Finally, if another Adobe Reader process is running right
  116. :: now, we won't be using the /T switch either.
  117. IF %MultiPrint% EQU 0 CALL :CheckProcess %PrintCmd%
  118. IF %MultiPrint% EQU 0 (
  119. 	SET PrintCmd=%PrintCmd:"%1"=/t "%%%~1"%
  120. 	rem SET PrintCmd=%PrintCmd% /t "%%~1"
  121. ) ELSE (
  122. 	SET PrintCmd=%PrintCmd:"%1"="%%%~1"%
  123. 	rem SET PrintCmd=%PrintCmd% "%%~1"
  124. )
  125. GOTO:EOF
  126.  
  127.  
  128. :CheckProcess
  129. IF "%ListTool%"=="" (
  130. 	SET MultiPrint=1
  131. ) ELSE (
  132. 	%ListTool% 2>&1 | FIND /I "%~n1" >NUL && SET MultiPrint=1
  133. )
  134. GOTO:EOF
  135.  
  136. :Syntax
  137. ECHO.
  138. ECHO PrintPDF.bat,  Version 3.11 for Windows NT 4 / 2000 / XP / Server 2003
  139. ECHO Prints PDF files from the command line
  140. ECHO.
  141. ECHO Usage:  PRINTPDF  pdf_filespec  [ /M ]
  142. ECHO.
  143. ECHO Where:  "pdf_filespec"  is the file name or filespec of the PDF file(s)
  144. ECHO                         to be printed; wildcards allowed (*); use double
  145. ECHO                         quotes for long file names
  146. ECHO.
  147. ECHO Notes:  This batch file has been tested with Acrobat Reader versions 5-7 only.
  148. ECHO         It requires Adobe/Acrobat Reader, and will NOT work if Acrobat "Writer"
  149. ECHO         is installed.
  150. ECHO         Thanks to Fabio Quieti, as of version 3.00, you no longer need to close
  151. ECHO         the minimized Acrobat Reader window manually, after printing the file.
  152. ECHO         Thanks to Michael Butler, printing lots of PDFs will no longer make the
  153. ECHO         computer slow down to a crawl or even hang.
  154. ECHO.
  155. ECHO Written by Rob van der Woude
  156. ECHO http://www.robvanderwoude.com
  157.  
  158.  
  159. :End
  160. IF "%OS%"=="Windows_NT" ENDLOCAL
  161.  

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