Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for cdrom3.bat

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

  1. @ECHO OFF
  2. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  3. REM * CDROM.BAT,  Version 3.00                              *
  4. REM * Read the CD-ROM drive letter from Windows' registry   *
  5. REM * Written by Rob van der Woude                          *
  6. REM *                                                       *
  7. REM * Sorry, for 32-bit Windows only.                       *
  8. REM *                                                       *
  9. REM * Inspired by Tom Lavedas' batch file with the same     *
  10. REM * functionality, which was adapted by Bill James.       * 
  11. REM * I added a second pass through the registry to skip    *
  12. REM * a ZIP drive, plus separate sections for NT 4 & 2000.  *
  13. REM * The CHOICE trick to strip the square brackets from    *
  14. REM * the registry entry's name came from "Outsider".       *
  15. REM *                                                       *
  16. REM * Thanks, guys.                                         *
  17. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  18.  
  19. REM * * * * * * * * * *
  20. REM * Recursive calls *
  21. REM * * * * * * * * * *
  22. IF "%1"=="Environment" GOTO Environment
  23. IF "%1"=="Loop"        GOTO Loop
  24.  
  25. REM * * * * * * * * * * * * * * * * * *
  26. REM * For Windows 95/98/NT4/2000 only *
  27. REM * * * * * * * * * * * * * * * * * *
  28. VER | FIND "Windows 9" >NUL
  29. IF NOT ERRORLEVEL 1 GOTO Win9x
  30. VER | FIND "Windows NT" >NUL
  31. IF NOT ERRORLEVEL 1 GOTO NT
  32. VER | FIND "Windows 2000" >NUL
  33. IF NOT ERRORLEVEL 1 GOTO W2K
  34. ECHO Sorry, for Windows 95/98/NT4/2000 only
  35. GOTO End
  36.  
  37. :: For Windows NT4
  38. :NT
  39. :: Find all registry sub-keys of HKEY_LOCAL_MACHINE\SYSTEM\DISK
  40. START /WAIT REGEDIT /E %TEMP%.\CDROM1.REG "HKEY_LOCAL_MACHINE\SYSTEM\DISK"
  41. :: Find the CD-ROM drives and parse the strings using a subroutine
  42. FOR /F "tokens=*" %%A IN ('TYPE %TEMP%.\CDROM1.REG ^| FIND /I "\\Device\\CdRom"') DO CALL :ParseNT4 %%A
  43. :: Remove temporary files
  44. GOTO Cleanup
  45.  
  46. :: Parse lines from REG file and store the results in environment variables
  47. :ParseNT4
  48. :: Store command line parameters in temporary variable LINE
  49. SET Line=%*
  50. :: Strip quotes
  51. SET Line=%Line:"=%
  52. :: Strip leading space in NT 4 only
  53. VER | FIND "Windows NT" >NUL
  54. IF NOT ERRORLEVEL 1 SET Line=%Line:~1%
  55. :: Remove quotes, backslashes and "Device" and store drive
  56. :: letters in environment variables named after CD-ROM devices
  57. FOR /F "tokens=2,3 delims=\= " %%A IN ('ECHO.%Line%') DO SET %%A=%%B
  58. :: Once more to display the result
  59. FOR /F "tokens=2,3 delims=\= " %%A IN ('ECHO.%Line%') DO ECHO CD-ROM drive detected, drive letter assigned is %%B
  60. :: Flush temporary variable
  61. SET LINE=
  62. GOTO:EOF
  63.  
  64.  
  65. :: For Windows 2000
  66. :W2K
  67. SETLOCAL
  68. SET CDROMS=
  69. SET Temp1=%Temp%.\%~n0%Random%1.reg
  70. SET Temp2=%Temp%.\%~n0%Random%2.reg
  71. START /WAIT REGEDIT /E %Temp1% "HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices"
  72. TYPE %Temp1% > %Temp2%
  73. TYPE %Temp2% | FIND "\\DosDevices\\" | FIND /V "\\DosDevices\\A:" | FIND "=hex:5c," > %Temp1%
  74. FOR /F "tokens=3 delims=\:" %%A IN (%Temp1%) DO CALL :ParseW2K %%A
  75. SET CDROMS
  76. DEL %Temp1%
  77. DEL %Temp2%
  78. ENDLOCAL & SET CDROMS=%CDROMS%
  79. GOTO:EOF
  80.  
  81. :ParseW2K
  82. IF DEFINED CDROMS (SET CDROMS=%CDROMS%,%1:) ELSE (SET CDROMS=%1:)
  83. GOTO:EOF
  84.  
  85.  
  86. REM * * * * * * * * * * *
  87. REM * For Windows 95/98 *
  88. REM * * * * * * * * * * *
  89. :Win9x
  90. REM * * * * * * * * * * * * * * * * * * * * * * * *
  91. REM * Make sure there is enough environment space *
  92. REM * * * * * * * * * * * * * * * * * * * * * * * *
  93. %COMSPEC% /E:2048 /C %0 Environment
  94. SET CMDLINE=
  95. CALL %TEMP%.\CDROM1.BAT
  96. GOTO Cleanup
  97.  
  98. :Environment
  99. REM * * * * * * * * * *
  100. REM * Reset variables *
  101. REM * * * * * * * * * *
  102. SET CDROM=
  103. SET KEY=
  104.  
  105. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  106. REM * Find all registry sub-keys of HKEY_LOCAL_MACHINE\Enum\SCSI  *
  107. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  108. START /WAIT REGEDIT /E %TEMP%.\CDROM1.REG "HKEY_LOCAL_MACHINE\Enum\SCSI"
  109.  
  110. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  111. REM * Extract the registry sub-key that is NOT the ZIP    *
  112. REM * drive and store it in the environment variable KEY  *
  113. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  114. TYPE %TEMP%.\CDROM1.REG|FIND "["|FIND /V "_ZIP"|FIND /V "SCSI]"|FIND /V "&">%TEMP%.\CDROM1.TMP
  115. ECHO.>>%TEMP%.\CDROM1.TMP
  116. TYPE %TEMP%.\CDROM1.TMP|DATE|FIND "):"|FIND "[">%TEMP%.\CDROM1.BAT
  117.  
  118. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  119. REM * Temporary batch files VOER.BAT and TYP.BAT are for Dutch Win9x  *
  120. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  121. ECHO SET KEY=%%6>VOER.BAT
  122. ECHO SET KEY=%%4>TYP.BAT
  123. ECHO SET KEY=%%3>CURRENT.BAT
  124. CALL %TEMP%.\CDROM1.BAT
  125.  
  126. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  127. REM * Strip the square brackets [] from the KEY variable  *
  128. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  129. ECHO ]|CHOICE /C:%KEY%; %0 Loop >%TEMP%.\CDROM1.BAT
  130. SET KEY=
  131. CALL %TEMP%.\CDROM1.BAT
  132. GOTO End
  133.  
  134. :Second
  135. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  136. REM * Find all registry sub-keys of the registry entry found earlier  *
  137. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  138. START /W REGEDIT /E %TEMP%.\CDROM1.REG "%KEY%"
  139.  
  140. REM * * * * * * * * * * * * * * * * * * * * * * *
  141. REM * Check if this really is the CD-ROM entry  *
  142. REM * * * * * * * * * * * * * * * * * * * * * * *
  143. TYPE %TEMP%.\CDROM1.REG|FIND "Class"|FIND "CDROM">NUL
  144. IF ERRORLEVEL 1 GOTO NoCdRom
  145.  
  146. REM * * * * * * * * * * * * * * * * * * * * * * * *
  147. REM * Read the drive letter assignment and store  *
  148. REM * store it in the environment variable CDROM  *
  149. REM * * * * * * * * * * * * * * * * * * * * * * * *
  150. TYPE %TEMP%.\CDROM1.REG|FIND "CurrentDriveLetterAssignment">%TEMP%.\CDROM1.TMP
  151. ECHO.>> %TEMP%.\CDROM1.TMP
  152. TYPE %TEMP%.\CDROM1.TMP|DATE|FIND "):"|FIND "Assignment">%TEMP%.\CDROM1.BAT
  153.  
  154. REM * * * * * * * * * * * * * * * * * * * * * * * *
  155. REM * Make sure there is enough environment space *
  156. REM * * * * * * * * * * * * * * * * * * * * * * * *
  157. SET KEY=
  158. SET CMDLINE=
  159.  
  160. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  161. REM * Temporary batch files VOER.BAT and TYP.BAT are for Dutch Win9x  *
  162. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  163. ECHO SET CDROM=%%7>VOER.BAT
  164. ECHO SET CDROM=%%5>TYP.BAT
  165. ECHO SET CDROM=%%4>CURRENT.BAT
  166. CALL %TEMP%.\CDROM1.BAT
  167.  
  168. REM * * * * * * * * * * * * * * * * * *
  169. REM * If variable not set, aparently  *
  170. REM * no CD-ROM drive was detected    *
  171. REM * * * * * * * * * * * * * * * * * *
  172. IF "%CDROM%"=="" GOTO NoCdRom
  173.  
  174. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  175. REM * Strip double quotes "" from the CDROM variable and create *
  176. REM * a temporary batch file to change the master environment   *
  177. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  178. ECHO @ECHO OFF>%TEMP%.\CDROM1.BAT
  179. FOR %%A IN (%CDROM%) DO ECHO SET CDROM=%%A:>>%TEMP%.\CDROM1.BAT
  180. CALL %TEMP%.\CDROM1.BAT
  181.  
  182. REM * * * * * * * * * * * *
  183. REM * Display the result  *
  184. REM * * * * * * * * * * * *
  185. ECHO CD-ROM drive detected, drive letter assigned is %CDROM%
  186. GOTO End
  187.  
  188. :NoCdRom
  189. REM * * * * * * * * * * * *
  190. REM * Display the result  *
  191. REM * * * * * * * * * * * *
  192. ECHO No CD-ROM drive detected
  193. GOTO End
  194.  
  195. :Loop
  196. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  197. REM * Strip the square brackets [] from the KEY variable  *
  198. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  199. SHIFT
  200. IF "%1"=="]" GOTO Second
  201. IF NOT "%1"=="[[" SET KEY=%KEY%%1
  202. GOTO Loop
  203.  
  204. :Cleanup
  205. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  206. REM * Clean up the ugly mess in the TEMP directory and the environment  *
  207. REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  208. SET KEY=
  209. SET CMDLINE=
  210. IF EXIST CURRENT.BAT      DEL CURRENT.BAT
  211. IF EXIST TYP.BAT          DEL TYP.BAT
  212. IF EXIST VOER.BAT         DEL VOER.BAT
  213. IF EXIST %TEMP%.\CDROM1.* DEL %TEMP%.\CDROM1.*
  214.  
  215. :End
  216.  

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