none
Batch script to detect drive letter of a USB flash drive.

    Question

  • Hi, 

    can you guys pls help me with a script to detect the USB drive letter of a flash drive. 

    I was searching the forum here and I found one but it does not work. 

    Here is what the script looks like. It is a batch script. 

    @echo off


    setlocal EnableDelayedExpansion

    set PNPDeviceID=4002FDCCE0E4D094

    set Q='wmic diskdrive where "interfacetype='USB' and PNPDeviceID like '%%%PNPDeviceID%%%'" assoc /assocclass:Win32_DiskDriveToDiskPartition'

    echo %Q%

    for /f "tokens=2,3,4,5 delims=,= " %%a in (%Q%) do (

      set hd=%%a %%b, %%c %%d

      call :_LIST_LETTER !hd!)

    goto :_END


    :_LIST_LETTER

    (echo %1 |find  "Disk ") >nul|| goto :_EOF 

    for /f "tokens=3 delims==" %%a in ('WMIC Path Win32_LogicalDiskToPartition  ^|find %1') do set TMP_letter=%%a

    set Part_letter=%TMP_letter:~1,2% 

    echo %Part_letter% %1

    goto :_EOF



    :_END

    :_EOF

    :: *** end

    pause

     

    When I run the above script. I get an error as this. 

    'wmic diskdrive where "interfacetype='USB' and PNPDeviceID like '%4002FDCCE0E4D0

    94%'" assoc /assocclass:Win32_DiskDriveToDiskPartition'

    No Instance(s) Available.

    Press any key to continue . . .

    Can someone pls tell me how to fix this issue. I am executing this script on a Vista machine. 

    The idea is to copy some configuration files from USB drive to the local computer. Since the USB drive letter varies in computers. I need to have a script that detects the drive letter of USB. 

    Thanks

    Mhndr

    Monday, June 07, 2010 9:13 PM

Answers

  • It expected input on the command line.  If you're going to just double-click on it, it needs a tiny bit more code ...

    @echo off
      setlocal
      set vName=%1
      if [%1]==[] set /p vName=Enter volume to search for:
      set n=0
      set ltrs=ABCDEFGHIJKLMNOPQRSTUVWXYZ
     :Loop
      call set ltr=%%ltrs:~%n%,1%%
      set /a n +=1
      vol %ltr%: 2>nul|find /i " %vname%">nul||if %n% lss 26 goto :loop
      if %n% equ 26 (set "ltr="
        echo No matching volume label found.
      ) else (echo Volume %1 is drive %ltr%:)
      endlocal & set ltr=%ltr%
      pause

    Tom Lavedas
    • Marked as answer by Mhndr Monday, June 14, 2010 2:58 PM
    Friday, June 11, 2010 7:48 PM

All replies

  • If you are using exactly the same USB device every time, why not search by volume name (or serial no.) instead?

    I have a script to detect a ramdisk, but that's written in vbscript/jscript.

    Friday, June 11, 2010 5:25 PM
  • can you pls tell me the command to search by volume name?

     

    Friday, June 11, 2010 5:28 PM
  • Here's one way to do it with a batch file ...

     @echo off
      if [%1]==[] echo Volume label input missing & goto :eof
      setlocal
      set n=0
      set ltrs=ABCDEFGHIJKLMNOPQRSTUVWXYZ
     :Loop
      call set ltr=%%ltrs:~%n%,1%%
      set /a n +=1
      vol %ltr%: 2>nul|find /i " %1">nul||if %n% lss 26 goto :loop
      if %n% equ 26 (set "ltr="
        echo No matching volume label found.
      ) else (echo Volume %1 is drive %ltr%:)
      endlocal & set ltr=%ltr%


    Tom Lavedas
    Friday, June 11, 2010 6:29 PM
  • You could run diskpart.exe, then process the output it generates. It's not a particularly elegant method but it will report which disks are USB mass storage devices and what their drive letters are:

    DISKPART> detail disk
    JetFlash Transcend 2GB USB Device
    Disk ID: 40794078
    Type   : USB
    . . .
      Volume ###  Ltr  Label        Fs     Type
      ----------  ---  -----------  -----  ----------
      Volume 4     H    2 GB Flash   NTFS   Removable

    DISKPART> detail disk
    Sony Storage Media USB Device
    Disk ID: C3072E18
    Type   : USB
      Volume ###  Ltr  Label        Fs     Type
      ----------  ---  -----------  -----  ----------
      Volume 5     G    LARGE USB    NTFS   Partition

    To generate the above report I used this command:
    diskpart < diskpart.scr

    The contents of diskpart.scr were:
    select disk 1
    detail disk
    select disk 2
    detail disk

    Friday, June 11, 2010 7:26 PM
  • Do I have to change anything on this script??

    When I run it as is. I get an error. 

     

    The screen closes instantly when I click on the batch file. 

    Friday, June 11, 2010 7:28 PM
  • It expected input on the command line.  If you're going to just double-click on it, it needs a tiny bit more code ...

    @echo off
      setlocal
      set vName=%1
      if [%1]==[] set /p vName=Enter volume to search for:
      set n=0
      set ltrs=ABCDEFGHIJKLMNOPQRSTUVWXYZ
     :Loop
      call set ltr=%%ltrs:~%n%,1%%
      set /a n +=1
      vol %ltr%: 2>nul|find /i " %vname%">nul||if %n% lss 26 goto :loop
      if %n% equ 26 (set "ltr="
        echo No matching volume label found.
      ) else (echo Volume %1 is drive %ltr%:)
      endlocal & set ltr=%ltr%
      pause

    Tom Lavedas
    • Marked as answer by Mhndr Monday, June 14, 2010 2:58 PM
    Friday, June 11, 2010 7:48 PM
  • The screen closes instantly when I click on the batch file. 

    Edit batch file with any common script editor (notepad, etc, etc) and on a separate line append the following

    pause

    on the very last line.  It would be really helpful if we were able to get the output that your program was producing.

    Friday, June 11, 2010 7:49 PM
  • When you test a batch file then it is compulsory to run it off a Command Prompt. If you run it off the Run box then you never see the error messages.

    Note also that the output I posted needs to be massaged in order to extract the drive letters.

    • Proposed as answer by RBode Saturday, November 13, 2010 5:31 PM
    Friday, June 11, 2010 8:42 PM
  • @echo off
    setlocal EnableDelayedExpansion 
    set _Alphabeth=ABCDEFGHIJKLMNOPQRSTUVWXYZ
    
    set _Number=0
    :Loop
    	call set _Letter=%%_Alphabeth:~%_Number%,1%%
    	if "%_Letter%"=="" goto ExitLoop
    	for /f "tokens=*" %%a in ('vol %_Letter%: 2^>nul ^| find " %_Letter% "') do (
    		setlocal
    		set _Volume=%%a
    		set _Volume=!_Volume:* %_Letter% =!
    		set _Volume=!_Volume:* =!
    		set !_Volume!=!_Letter!:
    	)
    	set /a _Number += 1
    Goto :Loop
    :ExitLoop
    set _Alphabeth=
    set _Letter=
    set _Number=
    set _Volume=
    @echo on
    
    if defined Voyager RoboCopy %Voyager%\Programs \\LaCie\openshare\Programs /mir
    

    This snippet creates an environment variable for every volume label which refers to the drive letter.

    The last line is an example (the volume label of my USB drive is: "Voyager")

    I  have tried to make it as multilingual as possible (also knowning that volume names might include spaces)...

    Saturday, November 13, 2010 5:37 PM
  • Let's simplify, shall we?

    @echo off

    for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do ( if %%l equ 2 ( echo %%i is a USB drive. ) ) Pause




    • Edited by sparky3489 Saturday, November 23, 2013 4:24 AM
    Friday, November 22, 2013 10:12 PM