In this “How To” we are going to write a simple bat file that will copy your user folder (My Documents, Favorites, Desktop, …) to another drive space (Network Share, USB External Hard Drive, …). The first time this script runs it does a full back up and depending on how large your user folder is it could take a long time. The next time you run the script it will only grab new or updated files making the copy that much faster. What’s nice about this script is that you can build onto it with other directories (Game Saves, Program Settings).
- Open Notepad.
- Copy the following code into Notepad.
@echo off :: variables set drive=E:\ set backupcmd=xcopy /s /c /d /e /h /i /r /k /y echo ### Backing up Documents... %backupcmd% "C:\Users\ENTER USERNAME HERE" "%drive%" echo Backup Complete! @pause
- Go to File/Save As.
- Change the “Save as type” to “All Files”.
- Now save this file to your desktop as xcopy backup.bat.
Now before we run this script we need to edit the code to fit with your situation. Right click your script and select “Edit”, it should now open in Notepad. Let’s look at a few lines of code to better understand what is taking place here.
In this first set of code we are creating two variables, drive and backupcmd. You are going to want to set “drive” to were you want your backup to go, in this example our backup is being copied to the E:\ drive. The backupcmd variable is simply our xcopy command with a few triggers, these triggers help copy only new files or updated files and also helps skip any read only errors that may come up. If you want a better explanation of what these triggers are, open Command Prompt and type xcopy /? and press enter.
:: variables set drive=E:\ set backupcmd=xcopy /s /c /d /e /h /i /r /k /y
The next batch of code is were the action happens. The first line is simply printing “### Backing up Documents…”, just to let the user know what it’s doing. When you add other directories to backup here you will want to include these messages to see how far into the script it is, like “###Backing up Diablo II…” The next line of code we get to see our two variables in action. First we run our backupcmd command (the xcopy with triggers), then we tell what directory we are backing up, in this example our user folder. BTW where it says “ENTER USERNAME HERE” do just that, enter your user name. If you are running Windows XP or older then you need to change this line to C:\Documents and Settings\ENTER USERNAME HERE. Then this line of code finishes it off with our drive variable. So ultimately we are telling command prompt to xcopy our user folder to our backup drive space (Example: drive E:\). The last line simply tells the user that your backup has completed.
echo ### Backing up Documents... %backupcmd% "C:\Users\ENTER USERNAME HERE" "%drive%" echo Backup Complete!
Now that your script has been edited to your liking, go ahead and save the final script and run it from your desktop by double clicking on it. You will see Command Prompt open and a list of files copying over to your backup drive space. It will tell you “Backup Complete!” when it finishes. Below is the same script with added directories to give you a better idea on how far you can take this.
@echo off :: variables set drive=E:\Ryan's Backup set backupcmd=xcopy /s /c /d /e /h /i /r /k /y echo ### Backing up Documents... %backupcmd% "C:\Users\ryanj" "%drive%" echo ### Backing up Diablo II... %backupcmd% "C:\Program Files\Diablo II\save" "%drive%" echo ### Backing up Local Websites... %backupcmd% "C:\wamp\www" "%drive%" echo Backup Complete! @pause