Detect USB and copy *.* to USB drive using batch script

前端 未结 4 709
野性不改
野性不改 2020-12-15 14:13

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 anoth

相关标签:
4条回答
  • 2020-12-15 14:27

    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=
    
    0 讨论(0)
  • 2020-12-15 14:28

    @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.

    0 讨论(0)
  • 2020-12-15 14:41

    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.

    0 讨论(0)
  • 2020-12-15 14:49
    @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^)
             )
          )
       )
    )
    
    0 讨论(0)
提交回复
热议问题