<http://www.netikka.net/tsneti/info/tscmd054.htm>
Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Mon 10-May-2010 13:33:56

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



54} How can I get the type of a disk device?

The following code will list all the devices that are present and their types. Requires administrator privileges.
  @echo off & setlocal enableextensions
  (for %%d in (A B C D E F G H I J K L M
               N O P Q R S T U V W X Y Z) do ^
    @fsutil fsinfo drivetype %%d:) ^
      | find /v "No such Root Directory"
  endlocal & goto :EOF

The output might be e.g.
  C:\_D\TEST>cmdfaq
  A: - Removable Drive
  C: - Fixed Drive
  D: - Removable Drive
  E: - Removable Drive
  N: - Removable Drive
  Z: - CD-ROM Drive

Anoter method, another PC
  @echo off & setlocal enableextensions
  set temp_=%temp%
  if defined mytemp set temp_=%mytemp%
  set outfileUnicode=%temp_%\infou.log
  set outfileAnsi=%temp_%\infoa.log
  "C:\WINDOWS\system32\dllcache\msinfo32.exe" /report "%outfileUnicode%" /categories +componentsstoragedrives
  type "%outfileUnicode%">"%outfileAnsi%"
  findstr "^Drive ^Description" "%outfileAnsi%"
  for %%f in ("%outfileUnicode%" "%outfileAnsi%") do if exist %%f del %%f
  endlocal & goto :EOF

The output might be e.g.
  Drive   A:
  Description     3 1/2 Inch Floppy Drive
  Drive   C:
  Description     Local Fixed Disk
  Drive   D:
  Description     Removable Disk
  Drive   L:
  Description     Network Connection
  Drive   N:
  Description     Removable Disk
  Drive   P:
  Description     Network Connection
  Drive   T:
  Description     Network Connection
  Drive   Y:
  Description     Network Connection
  Drive   Z:
  Description     CD-ROM Disc

For more comprehensive information we could substitute the following findstr line into the code
  findstr "^Drive ^Description ^Size ^Free\ Space ^Volume\ Name" "%outfileAnsi%"

Utilizing Visual Basic in the CMD script we have the following option:
  @echo off & setlocal enableextensions
  :: Build a Visual Basic Script
  set skip=
  set vbs_=%temp%\tmp$$$.vbs
  findstr "'%skip%VBS" "%~f0" > "%vbs_%"
  ::
  :: Check the usage

  if "%1"=="" (
    echo Usage: %~f0 [DiskDevice:]
    goto :EOF)
  for %%d in (a: b: c: d: e: f: g: h: i: j: k: l: m: n:
              o: p: q: r: s: t: u: v: w: x: y: z:) do (
              if /i "%%d"=="%1" goto _set)
  echo Usage: %~f0 [DiskDevice:]
  echo The DiskDevice must be A: - Z:
  goto :EOF
  ::
  :_set
  set drive_=%1
  ::
  :: Run the Visual Basic Script

  for /f "tokens=*" %%t in ('
    cscript //nologo "%vbs_%" "%drive_%"') do (
      set drivetype_=%%t)
  for %%f in ("%vbs_%") do if exist %%f del %%f
  ::
  :: Show the results

  echo Drive %drive_% is of type %drivetype_%
  endlocal & goto :EOF
  '
  '................................................................
  'The Visual Basic Script

  '
  Dim FSO, drive, d, dtype 'VBS
  Set WshShell = WScript.CreateObject("WScript.shell") 'VBS
  drive=WshShell.ExpandEnvironmentStrings("%drive_%") 'VBS
  Set FSO = CreateObject("Scripting.FileSystemObject") 'VBS
  If Not FSO.DriveExists(drive) Then 'VBS
    dtype = "Not found" 'VBS
  Else 'VBS
    Set d = FSO.GetDrive(drive) 'VBS
    Select Case d.DriveType 'VBS
      Case 0: dtype = "Unknown" 'VBS
      Case 1: dtype = "Removable" 'VBS
      Case 2: dtype = "Fixed" 'VBS
      Case 3: dtype = "Network" 'VBS
      Case 4: dtype = "CD-ROM" 'VBS
      Case 5: dtype = "RAM Disk" 'VBS
      Case Else dtype = "Not recognized" 'VBS
    End Select 'VBS
    If d.IsReady Then 'VBS
      dtype = dtype & "; Ready" 'VBS
    Else 'VBS
      dtype = dtype & "; Not ready" 'VBS
    End if 'VBS
  End If 'VBS
  WScript.Echo dtype 'VBS

The output could be e.g.
  D:\TEST>cmdfaq P:
  Drive P: is of type Network; Not ready

A pure script method, but (again) requiring administrator privileges, to test e.g. for a CD-ROM drive is
  @echo off & setlocal enableextensions
  (fsutil fsinfo drivetype Z:|find "CD-ROM Drive">nul)^
    && echo Z: is a CD-ROM Drive || echo Z: is not a CD-ROM Drive
  endlocal & goto :EOF

The output could be e.g.
  D:\TEST>cmdfaq
  Z: is a CD-ROM Drive

A related question. How does one detect the drive letter of the CD-ROM drive on a PC? This method works only if you are logged on as "an administrator or a member of the Administrators group".
  @echo off & setlocal enableextensions enabledelayedexpansion
  for %%d in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
    %comspec% /c if exist %%d:\ (
      fsutil fsinfo drivetype %%d:|find "CD-ROM Drive">nul
      if !errorlevel! EQU 0 set cdletter=%%d
    )
  )
  echo CD-ROM found on drive %cdletter%:
  endlocal & goto :EOF

An example of the output:
  C:\_D\TEST>cmdfaq
  CD-ROM found on drive Z:

Another related question. What is the file system of e.g. the C: drive? As an aside note the possibility of spanning the FOR command over several lines.
  @echo off & setlocal enableextensions
  for /f "tokens=5" %%a in (
    'fsutil fsinfo volumeinfo C:\^|
      findstr "File System Name"') do (
        echo %%a)
  endlocal & goto :EOF

An example of the output:
  C:\_D\TEST>cmdfaq
  NTFS

Revisit item #53 for testing whether the drive is ready or not, and for listing the devices that are ready.

References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Feb 9 2003, 5:29 pm [M]
  Google Groups Jun 4 2008, 12:17 am [M]
  Google Groups Jun 5 2008, 7:19 am [M]
  Google Groups May 10 2010, 5:08 am [M]
  How to use System Information (MSINFO32) command-line tool switches
  hh ntcmds.chm::/fsutil.htm