Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for printany.bat

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

  1. @ECHO OFF
  2. :: For Windows NT 4 or later only
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Localize variables
  6. SETLOCAL
  7. SET PrintCmd=
  8. SET FileType=
  9. SET Temp=%Temp:"=%
  10.  
  11. :: Command line parsing
  12. IF NOT "%~2"=="" GOTO Syntax
  13. IF     "%~1"=="" GOTO Syntax
  14. IF    "%~n1"=="" GOTO Syntax
  15. IF    "%~x1"=="" GOTO Syntax
  16. ECHO."%~1" | FIND "/" >NUL && GOTO Syntax
  17. ECHO."%~1" | FIND "?" >NUL && GOTO Syntax
  18. ECHO."%~1" | FIND "*" >NUL && GOTO Syntax
  19. IF NOT EXIST "%~1" GOTO Syntax
  20.  
  21. :: Get the file association from the registry
  22. FOR /F "tokens=1* delims==" %%A IN ('ASSOC %~x1') DO (
  23. 	FOR /F "tokens=1 delims==" %%C IN ('FTYPE ^| FIND /I "%%~B"') DO (
  24. 		CALL :GetPrintCommand %%~C
  25. 	)
  26. )
  27.  
  28. :: Check if a print command was found
  29. IF NOT DEFINED PrintCmd GOTO NoAssoc
  30.  
  31. :: Print the file using the print command we just found
  32. CALL START /MIN "PrintAny" %PrintCmd%
  33.  
  34. :: Done
  35. GOTO End
  36.  
  37.  
  38. :GetPrintCommand
  39. :: Get the print command for this file type from the registry
  40. FOR /F "tokens=3*" %%D IN ('REG.EXE Query HKCR\%1\shell\print\command /ve 2^>NUL') DO SET PrintCmd=%%E
  41. IF NOT DEFINED PrintCmd GOTO:EOF
  42. :: "Unescape" the command
  43. SET PrintCmd=%PrintCmd:\"="%
  44. SET PrintCmd=%PrintCmd:""="%
  45. SET PrintCmd=%PrintCmd:\\=\%
  46. :: Remove double double quotes in file name if applicable
  47. ECHO.%PrintCmd% | FINDSTR.EXE /R /C:"\"%%1\"" >NUL && SET PrintCmd=%PrintCmd:"%1"="%%%~1"%
  48. GOTO:EOF
  49.  
  50.  
  51. :NoAssoc
  52. CLS
  53. ECHO.
  54. ECHO Sorry, this batch file works only for known file types with associated
  55. ECHO print commands defined in the registry hive HKEY_CLASSES_ROOT.
  56. ECHO No print command seems to be assiociated with %~x1 files on this computer.
  57. ECHO.
  58.  
  59.  
  60. :Syntax
  61. ECHO.
  62. ECHO PrintAny.bat,  Version 2.01 for Windows 7 and later
  63. ECHO Prints any known file type from the command line
  64. ECHO.
  65. ECHO Usage:  PRINTANY  file_to_print
  66. ECHO.
  67. ECHO Where:  "file_to_print"  is the name of the file to be printed
  68. ECHO                          (use double quotes for long file names)
  69. ECHO.
  70. ECHO Notes:  For this batch file to work, the file type's print command has
  71. ECHO         to be defined in the registry hive HKEY_CLASSES_ROOT, and that
  72. ECHO         print command must NOT use DDE for printing.
  73. ECHO         Unfortunately, most Office file types use DDE and hence won't
  74. ECHO         print with this batch file.
  75. ECHO.
  76. ECHO Written by Rob van der Woude
  77. ECHO http://www.robvanderwoude.com
  78.  
  79. :End
  80. IF "%OS%"=="Windows_NT" ENDLOCAL
  81.  

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