Link to home
Start Free TrialLog in
Avatar of NetRock6
NetRock6Flag for Canada

asked on

Batch File- Finding Drive Description

Hi ...

I have to install few programs (like: abc.exe, EFC.exe...etc) that are located in the folder ...\MonitorSetup\.
The installation method is different depending, If these programs are in USB drive or Localdrive.
I was wondering, if someone can help to create a batch file to automate the above task.
 I can get the drive letter and check for the programs using below code, but I do not know how to get the description also to match to the drive letter. Once, I know the matching description, the batch file can goto the USB section or LocalDrive to install the programs accordingly.

Thank You For Your Help.

@echo off
setlocal enabledelayedexpansion
for /f %%d in ('wmic logicaldisk get caption') do (
if exist "%%d\MonitorSetup\abc.exe" (
do set drvlet=%%d
echo "!drvlet!\MonitorSetup\"
)
)

Open in new window

Avatar of NVIT
NVIT
Flag of United States of America image

Do these help?
wmic logicaldisk get volumename
wmic logicaldisk get caption, volumename

Open in new window


To see everything WMIC can show, run this to send the results to file drives.txt. Then review the file drives.txt.
wmic logicaldisk >drives.txt

Open in new window

Avatar of NetRock6

ASKER

What I am trying to figure out is: how to find the right "description" after knowing the "caption".
Thank you for your help.
I guess depends on what is 'right' description for you?
I didn't include the Description  field in my WMIC example because it didn't seem 'descriptive' enough.
My example here has a usb drive connected on G:

Caption  Description         VolumeName 
C:       Local Fixed Disk    Host01
D:       CD-ROM Disc                    
E:       Local Fixed Disk    External01 
F:       Network Connection  Share01
G:       Local Fixed Disk    TOSHIBA EXT

Open in new window

For the USB Flash Drive the Description is "Removable disk".
The USB drives that are fat32 usb devices 32GB storage or greater,  the Description appears as  "Local Fixed Disk".
The USB drives that are fat32 usb devices 8GB storage appears as "Removable disk".

I am looking to match the found 'caption' with  "Removable disk" (For USB Drive) OR
                                                                                     "Local Fixed Disk" (For Local Partition).

Thanks.
Avatar of Bill Prew
Bill Prew

Try this and see if DriveType is a better attribute to base logic on.  Unfortunately I don't have a FAT32 formatted USB drive >= 32GB to test with...

Also not that the output from WMIC is best processed though two FOR statements since WMIC terminates output lines with a carriage return rather than a carriage return line feed sequence, so this cleans that up.

@echo off
setlocal EnableDelayedExpansion

rem Drive types from WMIC LogicalDisk:
rem   0 = Unknown
rem   1 = No Root Directory
rem   2 = Removable Disk
rem   3 = Local Disk
rem   4 = Network Drive
rem   5 = Compact Disc
rem   6 = RAM Disk

rem Search all disks looking for installation files, get drive letter and drive type
for /f "skip=2 tokens=*" %%A in ('wmic /output:stdout LogicalDisk get Caption^,DriveType /format:csv') do (
  for /f "tokens=2,3 delims=," %%a in ("%%A") do (
    if exist "%%a\MonitorSetup\abc.exe" do (
      set drvlet=%%a
      set drvtyp=%%b
      echo "!drvlet!\MonitorSetup\","!drvtyp!"
    )
  )
)

rem Proceed depending on drive type
if %drvtyp% EQU 2 (
  rem Treat as USB drive
) else (
  rem Treat as fixed drive
)

Open in new window

~bp
SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent Bill, THANK YOU so MUCH for your help.

After running your code, (the last comment), getting: 'do is not recognized as an internal....' error.
but do not know which one is causing this...

Thank You Again.
Sorry, you had an extra do that was harmless, and I accidentally moved it to a harmful place.  Change this line:

   if exist "%%a\MonitorSetup\abc.exe" do (

to:

   if exist "%%a\MonitorSetup\abc.exe" (

~bp
This is what I get with this code:
(I have hard drive C, network share Z, and kingston USB in D, you are looking for drive types 3 and 2)

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "tokens=1,2 delims=:" %%d in ('wmic logicaldisk get caption^,DriveType ^|findstr :') do (
  4.      set drvlet=%%d:
  5.      set drvtyp=%%e
  6. for /f "tokens=* delims= " %%a in ("!drvtyp!") do set drvtyp=%%a
  7. echo drvltr  is !drvlet! DriveType is !drvtyp!
  8. REM if exist "%%d\temp\dummy.txt" (
  9. if exist "!drvlet!\temp\dummy.txt" (
  10. echo "!drvlet!\MonitorSetup\"  %drvtyp%
  11. )
  12. )

OUTPUT:
C:\Temp>x
drvltr  is C: DriveType is 3
"C:\MonitorSetup\"
drvltr  is D: DriveType is 2
drvltr  is Z: DriveType is 4
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank YOU "arana" for your time and improvement.
Keep the Good JOB!