Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for printhtm2.bat

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

  1. @ECHO OFF
  2. SETLOCAL
  3.  
  4. :: Command line parsing
  5. ECHO.%* | FIND "?" >NUL
  6. IF NOT ERRORLEVEL 1 GOTO Syntax
  7. ECHO.%* | FIND "*" >NUL
  8. IF NOT ERRORLEVEL 1 GOTO Syntax
  9. IF NOT [%3]==[] GOTO Syntax
  10. :: Check first parameter (file to be printed)
  11. SET File2Print=%1
  12. IF NOT DEFINED File2Print GOTO Syntax
  13. SET File2Print=%File2Print:"=%
  14. SET File2Print="%File2Print%"
  15. IF NOT DEFINED File2Print GOTO Syntax
  16. IF NOT EXIST "%File2Print:"=%" GOTO Syntax
  17. :: Check second parameter (printer)
  18. SET Printer=%2
  19. IF NOT DEFINED Printer GOTO Print
  20. SET Printer=%Printer:"=%
  21. SET Printer="%Printer%"
  22. REGEDIT /E %TEMP%.\%~n0.dat "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers"
  23. TYPE %TEMP%.\%~n0.dat | FIND "[" | FIND /I %Printer% >NUL
  24. IF ERRORLEVEL 1 (
  25. 	ECHO Invalid printer name, using default printer
  26. 	SET Printer=
  27. )
  28. DEL %TEMP%.\%~n0.dat
  29.  
  30. :: Create temporary Kix file to "press" Print button
  31. > %TEMP%.\%~n0.kix ECHO.; Wait a few seconds for the Print dialog to appear
  32. >>%TEMP%.\%~n0.kix ECHO SLEEP 2
  33. >>%TEMP%.\%~n0.kix ECHO.; Press "Print" (Enter) using SendKeys function
  34. :: Replace "Print" with the actual title of the
  35. :: Print dialog for non-US Windows versions
  36. >>%TEMP%.\%~n0.kix ECHO IF SETFOCUS( "Print" ) = 0
  37. >>%TEMP%.\%~n0.kix ECHO    $RC = SENDKEYS( "{ENTER}" )
  38. >>%TEMP%.\%~n0.kix ECHO ENDIF
  39.  
  40. :Print
  41. :: Actual print command
  42. START RUNDLL32.EXE MSHTML.DLL,PrintHTML %File2Print% %Printer%
  43.  
  44. :: Call temporary Kix file to "press" Print button, then delete it
  45. START /WAIT KIX32.EXE %TEMP%.\%~n0.kix
  46. DEL %TEMP%.\%~n0.kix
  47.  
  48. :: Done
  49. GOTO End
  50.  
  51. :Syntax
  52. ECHO.
  53. ECHO PrintHTM.bat,  Version 2.00 for Windows NT 4 / 2000
  54. ECHO Prints a local HTML file from the command line
  55. ECHO.
  56. ECHO Usage:  %~n0  ^<html_file^>  [ ^<printer^> ]
  57. ECHO.
  58. ECHO Use quotes around the file and printer names if they contain spaces.
  59. ECHO This version uses Kix's SendKeys^( ^) function to "press" the Print
  60. ECHO button in the Print dialog. Make sure that you have Kix installed on
  61. ECHO your system and that KIX32.EXE can be found in the PATH.
  62. ECHO Modify this batch file if you are using a non-US Windows version
  63. ECHO ^(read the comment lines for more information^).
  64. ECHO.
  65. ECHO Written by Rob van der Woude
  66. ECHO http://www.robvanderwoude.com
  67.  
  68. :End
  69. ENDLOCAL
  70.  

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