Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

batch-file

usb

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.

like image 851
user1433266 Avatar asked Jun 03 '12 07:06

user1433266


People also ask

How do I automatically copy files to a flash drive?

To make it, you need to create a schedule backup with USB plug in feature first. Then, you just need to plug it into your computer and it will automatically backup files from/to USB drive.

Can you see USB transfer history?

Getting USB History With Single Powershell Command To do this, open powershell and type "Get-ItemProperty -Path HKLM:SYSTEMCurrentControlSetEnumUSBSTOR** | Select FriendlyName." Then press enter, and you will get the history of all USB devices that have been used on your computer.


1 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^)
         )
      )
   )
)
like image 140
Aacini Avatar answered Oct 16 '22 19:10

Aacini