Backing up to USB with a batch Script in Microsoft Windows

I have recently reentered the world of work and have been issued with a brand new computer that has Windows 10 on it. In years passed, I developed an aversion to using Windows and was frustrated at each institution that pressed a computer into my hands with this proprietary operating system on me.
But with age, my zealousness and my idealism have waned and I am much more comfortable with using Windows, particularly now that Windows has an embedded Ubuntu subsystem.
I find, too, that once in a while I find something about Windows that I genuinely like. With an increasing number of training materials on my hard drive I have become increasingly paranoid that I will suffer a hard drive failure and lose all of my materials. For the moment I have taken to backing up my data to an USB thumb drive. In this article I will show you my approach in the hope that it will provide you some marginal value.

Fixing the USB thumb drive Drive Letter

To make it easier for us to write our script, firstly, we can ensure that each time we mount our USB thumb drive it will mount to the same logical drive letter. In my case, I chose the Z: drive meaning that any other media won’t accidentally be mapped to the same drive letter accidentally.
To achieve this, first launch the Disk Management utility. You can achieve this by pressing the Windows key then typing diskmgmt.msc and choosing the result that is listed. Insert your USB thumb drive and see it appear in the list of drives in Disk Management.

Right click on the the drive and choose “Change Drive Letter and Paths…”. Then change the drive letter to Z:. Each time you mount your drive in Windows from now on, it will mount to the drive letter Z.

Write the Script

Now that the drive is mounted predictably, a very simple batch script can be created. The purpose of this script will be to backup the contents of our Documents folder to the Z:\ and then un-mount the USB drive so we can pull it out as soon as the script has finished. As you can see, the script will give an error message if the Z: is not mounted. It uses ROBOCOPY to efficiently mirror the contents of the Documents directory to Z: and then to follow, it unmounts the volume.

if not exist z:\ (
    msg "%username%" Z drive is not mounted, please insert USB and try again
if exist z:\ (
    ROBOCOPY /R:1 /W:1 /MIR C:\Users\mijones\Documents Z:\
    mountvol z:\ /p
    msg "%username%" Backup successful. Z: drive unmounted

Paste the contents of the above into a text editor and save as a .bat on your desktop.  Whenever you want to backup, insert your USB stick and then double-click the script. When it has successfully unmounted it will tell you you can pull out the USB thumb drive.

Leave a Reply

Your email address will not be published. Required fields are marked *