Option Explicit Dim intAnswer, intButtons Dim colEvents, objEvent, objFSO, objWMIService, wshShell Dim strCLArgs, strDisk, strLogDir, strLogFile, strMessage, strSerial, strSourceDir, strTitle strLogFile = "" With WScript.Arguments If .Named.Count > 0 Then Syntax If .Unnamed.Count < 2 Then Syntax If .Unnamed.Count > 3 Then Syntax strSourceDir = .Unnamed.Item(0) strSerial = Replace( .Unnamed.Item(1), "-", "" ) If .Unnamed.Count = 3 Then strLogFile = .Unnamed.Item(2) End If End With If Len( strSerial ) <> 8 Then Syntax Set objFSO = CreateObject( "Scripting.FileSystemObject" ) If Not objFSO.FolderExists( strSourceDir ) Then Syntax If strLogFile <> "" Then strLogDir = objFSO.GetParentFolderName( strLogFile ) If Not objFSO.FolderExists( strLogDir ) Then Syntax End If Set wshShell = CreateObject( "WScript.Shell" ) Set objWMIService = GetObject( "winmgmts://./root/CIMV2" ) Set colEvents = objWMIService.ExecNotificationQuery( "SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE TargetInstance ISA 'Win32_LogicalDisk'" ) Do While True ' Endless loop Set objEvent = colEvents.NextEvent If objEvent.TargetInstance.VolumeSerialNumber = strSerial Then If objEvent.Path_.Class = "__InstanceCreationEvent" Then strDisk = objEvent.TargetInstance.DeviceID strTitle = "External Harddisk " & strDisk & " Connected" strMessage = "Do you want to start backing up to the external harddisk (drive " & strDisk & " """ & objEvent.TargetInstance.VolumeName & """) now?" intButtons = vbYesNoCancel + vbQuestion + vbSystemModal intAnswer = MsgBox( strMessage, intButtons, strTitle ) If intAnswer = vbCancel Then Exit Do If intAnswer = vbYes Then ' Replace /S by /MIR if you want to purge deleted files from the backup drive; type ROBOCOPY /? for help strCLArgs = """" & strSourceDir & """ """ & strDisk & Mid( strSourceDir, 3 ) & """ /S /XJ /COPY:DT /DCOPY:T /NP /W:0 /R:0" If strLogFile <> "" Then strCLArgs = strCLArgs & " /LOG:""" & strLogFile & """" ' Feel free to use any customized backup command or script instead of this hard-coded ROBOCOPY command wshShell.Run "CMD.EXE /C START ROBOCOPY.EXE " & strCLArgs, 7, False End If ElseIf objEvent.Path_.Class = "__InstanceDeletionEvent" Then strTitle = "External Harddisk " & strDisk & " Disconnected" strMessage = "The externe harddisk " & strDisk & " has been disconnected." & vbCrLf & vbCrLf & "If this was done before the backup was completed, please reconnect the external harddisk and restart the backup when prompted." intButtons = vbOKOnly + vbExclamation + vbSystemModal MsgBox strMessage, intButtons, strTitle End If End If Loop Set colEvents = Nothing Set objWMIService = Nothing Set wshShell = Nothing Set objFSO = Nothing Sub Syntax( ) Dim strHelp strHelp = vbCrLf _ & "Backup2ExternalHDD.vbs, Version 1.00" _ & vbCrLf _ & "Automatically start a backup whenever a specific external harddisk is connected" _ & vbCrLf & vbCrLf _ & "Usage: WSCRIPT.EXE Backup2ExternalHDD.vbs sourcedir volserial" _ & vbCrLf & vbCrLf _ & "Where: sourcedir directory to be backed up" _ & vbCrLf _ & " volserial external harddisk's volume serial number without hyphen" _ & vbCrLf & vbCrLf _ & "Notes: To look up the volume serial number of a drive, use the VOL command," _ & vbCrLf _ & " e.g. VOL L: to find the volume serial number of drive L:." _ & vbCrLf & vbCrLf _ & " If you want to use a rotating set of external harddisks for backups," _ & vbCrLf _ & " make their volume serial numbers identical using Microsoft's VolumeID" _ & vbCrLf _ & " tool: https://technet.microsoft.com/en-us/sysinternals/bb897436.aspx" _ & vbCrLf & vbCrLf _ & " Do NOT set the volume serial number to 0000-0000, that would make WMI" _ & vbCrLf _ & " return a blank volume serial number, and the backup would never run." _ & vbCrLf & vbCrLf _ & " The backup command used by this script is a hard-coded ROBOCOPY" _ & vbCrLf _ & " command; feel free to replace it by any other suitable command or" _ & vbCrLf _ & " batch file." _ & vbCrLf _ & " The backup command used by this script duplicates the source" _ & vbCrLf _ & " directory's folder structure on the backup drive; feel free to" _ & vbCrLf _ & " change it to suit your requirements." _ & vbCrLf & vbCrLf _ & " This script is intended to be be scheduled at logon." _ & vbCrLf & vbCrLf _ & "Written by Rob van der Woude" _ & vbCrLf _ & "http://www.robvanderwoude.com" WScript.Echo strHelp Set objFSO = Nothing WScript.Quit 1 End Sub