USB discovery and copy *. * To a USB drive using a script package

I am trying to write a script package to detect a USB drive and if it is connected, for example, copy the file c: \ test \ big.txt to a USB drive and run a loop to find another flash drive.

+4
source share
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 : The following is the correct way:

 @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^) ) ) ) ) 
+8
source

There was a flaw in the above code, in which the following code was involved. The code works in XP and gives you the letters of USB drives, if there is no USB device connected, this tells you that!

:: SUCCESS @ 2: 39 a.m. on October 12, 2013 !!! :: IMPROVED BOBBY GOREY

 @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= 
+1
source

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 suggests that, of course, both discs are inserted.

+1
source

@Aacini Today I don’t have a good setup for this case, so I tried instead to find my USB webcam. I used devmgmt.msc and devcon listclasses to determine the membership associated with the connected USB camera. After several tests, I came to devcon find =Image USB\* . I figured it would be simple enough to do the same for a USB storage device, so I tried devcon find =Volume (for each list). Unfortunately, this resets the GUID that you will need to map to the drive letter. A quick look at this overflow suggests that you can do this from the registry using reg query , but it seems that at this point fsutil will be easier for your business.

0
source

Source: https://habr.com/ru/post/1415794/


All Articles