I am trying to write a batch script to detect a USB drive and, if it is plugged in, for example copy c:\test\big.txt to the USB drive, and looping to detect another flash drive.
-
do you want to copy files or direcotries? – das_j Jun 03 '12 at 12:16
4 Answers
@echo off
for %%d in (D: E: F: G: H: I: etc...) do (
if exist %%d\nul (
echo USB at drive %%d connected
)
)
EDIT: Below is the right way to do that:
@echo off
for /F "tokens=1*" %%a in ('fsutil fsinfo drives') do (
for %%c in (%%b) do (
for /F "tokens=3" %%d in ('fsutil fsinfo drivetype %%c') do (
if %%d equ Removable (
echo Drive %%c is Removable (USB^)
)
)
)
)

- 65,180
- 12
- 72
- 108
-
2
-
Another alternative is to use Microsoft's [DevCon](http://support.microsoft.com/kb/311272) utility. – Stephen Niedzielski Jun 12 '12 at 04:54
-
@StephenNiedzielski: I see no reason to download and use an additional application if the problem may be solved with the installed commands. On the other hand, I reviewed DevCon description at that link and I can't find the way to identify the USB drives. Could you show us an example of how to do that? Thanks! – Aacini Jun 13 '12 at 02:12
-
@Aacini I ran out of room for a comment, so I've posted below. – Stephen Niedzielski Jun 13 '12 at 07:10
-
Is there any way to do the second option without administrator privileges? – MD XF Sep 30 '16 at 19:42
I know this is old but....
@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 (
copy c:\test\big.txt %%i
)
)
This is assuming of course both drives are inserted.

- 99
- 9
The above code had a flaw which has been attended to in the following code The code works in XP and gives you the USB drive letters, if no usb device is connected , it tells you so !
:: SUCCESS @ 2:39 AM ON 12 OCT 2013 !!! :: IMPROVED BY BOBBY GOREJA
@echo off
set usbdrv=
set usb=No
:: Above two lines on 12 Oct 2013
fsutil fsinfo drives >de
type de | find "Drives:" /v >dlist
for /F "tokens=1" %%c in ('type dlist') do (
for /F "tokens=3" %%d in ('fsutil fsinfo drivetype %%c') do (
rem echo Token is %%d
if %%d equ Removable (
echo Drive %%c is Removable (USB^)
set usbletter=%%c
set usb=Yes
echo USB drive letter is %usbletter%
rem set usbdrv = %%c <<< this does NOT work!
rem echo USB1 drive letter is %usbdrv%
)
)
)
del de
del dlist
echo REPEAT:Device at %usbletter%
if "%usb%"=="No" echo No USB Device Connected .
set usb=

- 11
- 1
@Aacini I don't have a good setup to this case today, so I instead tried finding my USB webcam. I used devmgmt.msc
and devcon listclasses
to figure out the membership a connected USB camera would have. After a few tests, I arrived at devcon find =Image USB\*
. I figured it would be simple enough to do the same for a USB mass storage device, so I tried devcon find =Volume
(per listclasses). Unfortunately, this dumps out a GUID which you would then have to map to a drive letter. A cursory glance at this overflow suggests you can do so from the registry using reg query
, but it seems at this point that fsutil
would be simplest for your case.

- 1
- 1

- 2,497
- 1
- 28
- 34