Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

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

$
0
0

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


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>