0

So I'm trying to make a bat file run automatically on Windows 10 to backup files to an external USB drive whenever the USB drive is plugged in. I've so far thought about doing a Windows Task Scheduler task, but as far as I know, Task Scheduler does not give the option to run a task when a specific device is attached to the computer (though if it does, please let me know and please also tell me how to do it as that would solve all the problems I'm having). I've also thought about doing this with an Autorun.inf, but Windows no longer supports that. So in lieu of either of those options, what is the best way to run a bat file for backup purposes when a specific USB drive is plugged in? P.S. Both for verification purposes that this is what I intend to do and if anyone needs to see it as a reference, below is a copy of the bat file I'm using for backup;

TITLE Backup of All Files (Excluding System Files) on the Main Drive C: 

powershell -window minimized -command ""

@ECHO OFF
 set dt=%DATE:~0,2%/%DATE:~3,2%/%DATE:~6,4%:%TIME:~0,2%.%TIME:~3,2%.

%TIME:~6,2%
 set dt=%dt: =0%
 ECHO %dt%>>backuplog-C.txt

robocopy C: %~dp0\C-MainDrive\ALL * /R:10 /E /W:15 /FFT /COPYALL /COPY:DAT /Z /TS /FP /XA:R /XD Temp Drivers "Program Files" "Program Files (x86)" found.000 found.001 found.002 msdownload.tmp Windows.old $WINDOWS.~WS $WinREAgent Documents and Settings $WINDOWS.~BT $Windows $GetCurrent $SysReset "#Windows" $RECYCLE.BIN "#RECYCLE BIN" "Documents and Settings" "System Volume Information" /XF hiberfil.sys pagefile.sys swapfile.sys /V

echo Backup completed at %dt%

robocopy Backup_Logs\ Backup_Logs\ >>Backup_Logs\backuplog_MainDrive.txt

powershell -Command "& {Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show('The backup of all your files on your Main Drive C: drive has completed successfully.', 'Backup Successful!', 'OK', [System.Windows.Forms.MessageBoxIcon]::Information);}"
Fenix001
  • 11
  • 2
  • 2
    Does this answer your question? [Starting scheduled task by detecting connection of USB device](https://superuser.com/questions/219401/starting-scheduled-task-by-detecting-connection-of-usb-device) – harrymc Sep 30 '21 at 12:57
  • Thanks for the suggestion, but no it doesnt. Im using Windows 10 (a fact which I forgot to include in the question when I posted it so I edited the question and added that). In addition, Id much prefer not to use the Autorun.inf (even though I mentioned it) because of the ris involved. Thanks again for the suggestion though! – Fenix001 Sep 30 '21 at 13:34
  • I think you completely misunderstood the linked answer: (1) It will work on Windows 10, (2) It doesn't use autorun.inf. – harrymc Sep 30 '21 at 13:40
  • Okay, I'll look at it again. – Fenix001 Sep 30 '21 at 14:26
  • Okay, so I took another look at it and it does seem to do what I want - sorry, about that. One thing Id like to clarify though: Does the PowerShell script have to be run manually each time the device is connected? Or how does that work? (Sorry if this is an obvious question, Im a total noob at coding/programming/scripting). Thanks again for the help! – Fenix001 Sep 30 '21 at 19:36
  • Not manually - the Task Scheduler does it for you. This is an advanced subject, unfortunately. – harrymc Sep 30 '21 at 19:38
  • *Caveat*: I find Windows 10 does not always recognize the connection of a USB device that has been inserted, removed, and then *reinserted*. For that reason, I would not trust that the script would always run. – DrMoishe Pippik Sep 30 '21 at 21:03
  • Try this vbscript instead or get inspired from it [AutoSave_USB_SDCard.vbs](https://pastebin.com/iMpQ995U) – Hackoo Oct 01 '21 at 20:32
  • @Hackoo Thanks for the suggestion, but is there a way to change the backup location (the place where all the files are copied to)? If there is, Id like to specify a specific location myself. Please let me know, thanks! – Fenix001 Oct 03 '21 at 12:02

0 Answers0