Powered by GeSHi

Source code for drives.bat

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

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Check if WMIC is available
  6. WMIC.EXE /? >NUL 2>&1 || GOTO Syntax
  7.  
  8. :: keep variables local
  9. SETLOCAL ENABLEDELAYEDEXPANSION
  10.  
  11. :: Command line parsing
  12. IF NOT "%~2"=="" GOTO Syntax
  13.  
  14. SET AcceptDriveTypes=0
  15. SET Numeric=0
  16. IF /I "%~1"==""   SET AcceptDriveTypes=23456
  17. IF /I "%~1"=="/C" SET AcceptDriveTypes=5
  18. IF /I "%~1"=="/F" SET AcceptDriveTypes=3
  19. IF /I "%~1"=="/L" SET AcceptDriveTypes=2356
  20. IF /I "%~1"=="/N" SET AcceptDriveTypes=4
  21. IF /I "%~1"=="/R" SET AcceptDriveTypes=25
  22. IF /I "%~1"=="/T" SET AcceptDriveTypes=23456
  23. IF /I "%~1"=="/T" SET Numeric=1
  24. IF %AcceptDriveTypes% EQU 0 (
  25. 	SET Arg=%~1
  26. 	IF /I "!Arg:~0,3!"=="/T:" (
  27. 		REM *** Add 1 as prefix, and remove it again, to
  28. 		REM *** prevent interpretation of leading zero as octal
  29. 		SET /A AcceptDriveTypes = 1!Arg:~3!
  30. 		SET AcceptDriveTypes=!AcceptDriveTypes:~1!
  31. 		SET Numeric=1
  32. 	)
  33. )
  34.  
  35. :: If AcceptDriveTypes is zero, an invalid command line argument was passed
  36. IF %AcceptDriveTypes% EQU 0 (
  37. 	ENDLOCAL
  38. 	GOTO Syntax
  39. )
  40.  
  41. :: WMIC query to list all drive letters and drive types
  42. FOR /F "tokens=2,3* delims=," %%A IN ('WMIC.EXE /Node:"%Node%" /Output:STDOUT Path Win32_LogicalDisk Get DeviceID^,Description^,DriveType /Format:CSV ^| FINDSTR /R /C:",[A-Z]:"') DO (
  43. 	REM Add an extra FOR loop to remove the linefeed from %%C
  44. 	FOR %%D IN (%%C) DO (
  45. 		ECHO.%AcceptDriveTypes% | FIND "%%~D" >NUL
  46. 		IF NOT ERRORLEVEL 1 (
  47. 			IF %Numeric% EQU 1 (
  48. 				ECHO.  %%B      %%C
  49. 			) ELSE (
  50. 				ECHO.  %%B      %%A
  51. 			)
  52. 		)
  53. 	)
  54. )
  55.  
  56. :: Done
  57. ENDLOCAL
  58. GOTO:EOF
  59.  
  60.  
  61. :Syntax
  62. SET AcceptDriveTypes=
  63. ECHO.
  64. ECHO Drives.bat,  Version 2.01 for Windows XP Professional and later
  65. ECHO List disk drives and their drive types
  66. ECHO.
  67. ECHO Usage:  DRIVES  [ /C ^| /F ^| /N ^| /R ]
  68. ECHO.
  69. ECHO Where:  /C      displays only CD-ROM drives
  70. ECHO         /F      displays only Fixed disk drives
  71. ECHO         /L      displays only Local disk drives ^(including removables^)
  72. ECHO         /N      displays only Network drives
  73. ECHO         /R      displays only Removable drives  ^(including CD-ROMs^)
  74. ECHO         /T[:n]  displays only numeric drive Types [of type n,m,..];
  75. ECHO                 n can be:   0 (Unknown),          1 (No Root Directory)
  76. ECHO                             2 (Removable Disk)    3 (Local Disk)
  77. ECHO                             4 (Network Drive)     5 (Compact Disk)
  78. ECHO                             6 (RAM Disk)          or any combination,
  79. ECHO                 e.g. 35 for local disks and CD-ROMs
  80. ECHO.
  81. ECHO Notes:  If no argument is used, all drives will be listed.
  82. ECHO         Removable drives that are currently unavailable may still be displayed.
  83. ECHO         This batch file uses WMIC, so it won't work in Windows 2000 or XP Home.
  84. ECHO.
  85. ECHO Written by Rob van der Woude
  86. ECHO http://www.robvanderwoude.com
  87.  
  88. IF "%OS%"=="Windows_NT" EXIT /B 1
  89.