Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for myprn2k.bat

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

  1. @ECHO OFF
  2. :: Check Windows version and command line arguments
  3. IF NOT "%1"=="" GOTO Syntax
  4. VER | FIND "Windows 2000" >NUL
  5. IF ERRORLEVEL 1 GOTO Syntax
  6.  
  7. :: Use local environment
  8. SETLOCAL
  9.  
  10. :: Export list of printers from the registry
  11. START /WAIT REGEDIT.EXE /E %Temp%.\_MyPrn.dat "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers"
  12.  
  13. :: Display header
  14. ECHO.
  15. ECHO Printers installed on this PC:
  16. ECHO.
  17. :: Name is followed by 14 spaces, Port is followed by 2 tabs
  18. ECHO Name             Port		Driver
  19. ECHO ----             ----		------
  20. :: Write header to output file
  21. > %~n0.dat ECHO Name	Share	Port	Driver	Comment	Location
  22.  
  23. :: List properties for each printer from the list
  24. FOR /F "tokens=7 delims=\" %%a IN ('TYPE %Temp%.\_MyPrn.dat ^| FIND "["') DO FOR /F "tokens=1 delims=]" %%A IN ('ECHO.%%a ^| FIND "]"') DO CALL :Enum "%%A"
  25.  
  26. :: Remove temporary file
  27. IF EXIST %Temp%.\_MyPrn.dat DEL %Temp%.\_MyPrn.dat
  28.  
  29. :: Done
  30. ENDLOCAL
  31. GOTO:EOF
  32.  
  33.  
  34. :Enum
  35. :: Export list of printer properties to temporary file
  36. START /WAIT RUNDLL32 PRINTUI.DLL,PrintUIEntry /f "%Temp%.\_MyPrn.txt" /Xg /n "%~1"
  37. :: Abort if export failed
  38. IF NOT EXIST "%Temp%.\_MyPrn.txt" GOTO:EOF
  39. :: Initialize variables
  40. SET Name=
  41. SET Share=
  42. SET Port=
  43. SET Driver=
  44. SET Comment=
  45. SET Location=
  46. :: Read single properties from file and store in variables
  47. FOR /F "tokens=1*" %%A IN ('TYPE "%Temp%.\_MyPrn.txt" ^| FIND "PrinterName:"') DO SET Name=%%B
  48. FOR /F "tokens=1*" %%A IN ('TYPE "%Temp%.\_MyPrn.txt" ^| FIND "ShareName:"')   DO SET Share=%%B
  49. FOR /F "tokens=1*" %%A IN ('TYPE "%Temp%.\_MyPrn.txt" ^| FIND "PortName:"')    DO SET Port=%%B
  50. FOR /F "tokens=1*" %%A IN ('TYPE "%Temp%.\_MyPrn.txt" ^| FIND "DriverName:"')  DO SET Driver=%%B
  51. FOR /F "tokens=1*" %%A IN ('TYPE "%Temp%.\_MyPrn.txt" ^| FIND "Comment:"')     DO SET Comment=%%B
  52. FOR /F "tokens=1*" %%A IN ('TYPE "%Temp%.\_MyPrn.txt" ^| FIND "Location:"')    DO SET Location=%%B
  53. :: Set display name Sname to printer name Name plus 16 spaces
  54. SET Sname=%Name%                
  55. :: Use first 16 characters of Sname only
  56. SET Sname=%Sname:~0,16%
  57. :: Display printer name, port and driver name
  58. ECHO.%Sname% %Port%		%Driver%
  59. :: Write printer properties to output file
  60. >> %~n0.dat ECHO.%Name%	%Share%	%Port%	%Driver%	%Comment%	%Location%
  61. :: Remove temporary file
  62. DEL "%Temp%.\_MyPrn.txt"
  63. GOTO:EOF
  64.  
  65.  
  66. :Syntax
  67. ECHO.
  68. ECHO MyPRN2K.bat,  Version 1.00 for Windows 2000 / XP
  69. ECHO Display a list of all printers installed on this PC
  70. ECHO.
  71. ECHO Usage:  MYPRN2K
  72. ECHO.
  73. ECHO The result is stored in a tab delimited file named
  74. ECHO MYPRN2K.DAT, located in the current directory.
  75. ECHO.
  76. ECHO Written by Rob van der Woude
  77. ECHO http://www.robvanderwoude.com
  78.  

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