Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for listprn.bat

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

  1. @ECHO OFF
  2. REM ListPRN,  Version 2.01 for Windows NT and OS/2 Warp
  3. REM Lists either specified or all network printers' UNC path
  4. REM Written by Rob van der Woude
  5. REM http://www.robvanderwoude.com
  6.  
  7. REM Keep all variables local:
  8. SETLOCAL
  9. ECHO.
  10.  
  11. REM Check command line parameters for question mark, show syntax when found:
  12. IF NOT [%1]==[] (ECHO.%1 | FIND "?" >NUL && GOTO Syntax)
  13.  
  14. REM OS/2 check:
  15. VER | FIND "/2" >NUL
  16. IF NOT ERRORLEVEL 1 GOTO OS2
  17.  
  18. :: NT check:
  19. VER | FIND "Windows NT" >NUL
  20. IF ERRORLEVEL 1 GOTO Syntax
  21.  
  22. :: NT
  23. :: Show all printer's UNC path, or specified printer's only:
  24. FOR /F "skip=3 eol=D tokens=1 delims=\ " %%A IN ('NET VIEW') DO FOR /F %%a IN ('NET VIEW \\%%A ^| FIND " Printer "') DO IF [%1]==[] (ECHO \\%%A\%%a) ELSE (IF /I [%1]==[%%a] ECHO \\%%A\%%a)
  25. GOTO End
  26.  
  27. :OS2
  28. REM Create temporary batch files:
  29. ECHO SET SRV=%%6>VOER.CMD
  30. ECHO SET SRV=%%4>TYP.CMD
  31. ECHO SET SRV=%%3>CURRENT.CMD
  32. ECHO SET SRV=%%5>ENTER.CMD
  33.  
  34. REM Create empty temporary file:
  35. TYPE NUL >%TEMP%.\NETPRN.LST
  36.  
  37. REM List all servers and store the list in a temporary server list:
  38. NET VIEW | FIND "\\\\" >%TEMP%.\NETVIEW.LST
  39. :Loop1
  40. REM Store the first word of the first line from the temporary
  41. REM server list in an environment variable by using the DATE trick:
  42. TYPE %TEMP%.\NETVIEW.LST | DATE | FIND "\\\\" >%TEMP%.\NETVIEW.CMD
  43. CALL %TEMP%.\NETVIEW.CMD
  44.  
  45. REM Ignore empty lines in the temporary server list:
  46. ECHO [%SRV%] | FIND "[\\\\" >NUL
  47. IF ERRORLEVEL 1 GOTO End
  48.  
  49. REM List all printers for each server to a temporary
  50. REM printer list (English as well as Dutch):
  51. NET VIEW %SRV% | FIND " Print" >>%TEMP%.\NETPRN.LST
  52. NET VIEW %SRV% | FIND " Afdrukken" >>%TEMP%.\NETPRN.LST
  53.  
  54. REM Remove the first line from the temporary server list:
  55. TYPE %TEMP%.\NETVIEW.LST | FIND /V "%SRV%" >%TEMP%.\NETVIEW2.LST
  56. DEL %TEMP%.\NETVIEW.LST
  57. REN %TEMP%.\NETVIEW2.LST NETVIEW.LST
  58.  
  59. REM Check if the temporary server list is empty; loop if not,
  60. REM otherwise go on processing the temporary printer list:
  61. TYPE %TEMP%.\NETVIEW.LST | FIND "\\\\" >NUL
  62. IF ERRORLEVEL 1 GOTO ListUNCs
  63. GOTO Loop1
  64.  
  65. :ListUNCs
  66. REM Adapt temporary batch files:
  67. ECHO SET NETPRN=%%6>VOER.CMD
  68. ECHO SET NETPRN=%%4>TYP.CMD
  69. ECHO SET NETPRN=%%3>CURRENT.CMD
  70. ECHO SET NETPRN=%%5>ENTER.CMD
  71.  
  72. REM Remove empty lines from temporary printer list:
  73. TYPE %TEMP%.\NETPRN.LST | FIND " " >%TEMP%.\NETPRN2.LST
  74. DEL %TEMP%.\NETPRN.LST
  75. REN %TEMP%.\NETPRN2.LST NETPRN.LST
  76. :Loop2
  77. REM Store the first word of the first line from the temporary
  78. REM printer list in an environment variable by using the DATE trick:
  79. TYPE %TEMP%.\NETPRN.LST | FIND " " | DATE | FIND ")" >%TEMP%.\NETPRN.CMD
  80. CALL %TEMP%.\NETPRN.CMD
  81.  
  82. REM Remove leading and trailing spaces from the environment variable:
  83. FOR %%A IN (%NETPRN%) DO SET TEST=%%A
  84.  
  85. REM Stop if there are no more lines left:
  86. IF [%TEST%]==[] GOTO Cleanup
  87.  
  88. REM Display UNC path:
  89. ECHO %SRV%\%NETPRN%
  90.  
  91. REM Remove the first line from the temporary printer list:
  92. TYPE %TEMP%.\NETPRN.LST | FIND /V "%NETPRN%" >%TEMP%.\NETPRN2.LST
  93. DEL %TEMP%.\NETPRN.LST
  94. REN %TEMP%.\NETPRN2.LST NETPRN.LST
  95.  
  96. REM Stop if there are no more lines left:
  97. TYPE %TEMP%.\NETPRN.LST | FIND " " >NUL
  98. IF ERRORLEVEL 1 GOTO Cleanup
  99. GOTO Loop2
  100.  
  101. :Cleanup
  102. REM Delete OS/2's temporary files
  103. FOR %%A IN (CURRENT ENTER TYP VOER) DO IF EXIST %%A.CMD DEL %%A.CMD
  104. FOR %%A IN (NETVIEW NETPRN) DO FOR %%B IN (CMD LST) DO IF EXIST %TEMP%.\%%A?.%%B DEL %TEMP%.\%%A?.%%B
  105. GOTO End
  106.  
  107. :Syntax
  108. ECHO ListPRN,  Version 3.00 for OS/2 Warp and Windows NT
  109. ECHO Displays full UNC path of specified or all network printer(s)
  110. ECHO Written by Rob van der Woude
  111. ECHO http://www.robvanderwoude.com
  112. ECHO.
  113. ECHO Usage:  LISTPRN  [ ^<printer_share_name^> ]
  114.  
  115. :End
  116. ENDLOCAL
  117.  

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