Tutorial Thursday

How To Create A Batch File For Synchronizing FoldersI have multiple computers around the house and one of my greatest fears is that a computer will crash and I will lose all of my pictures, music, and documents. The time it would take to try and recover all of the information is far longer than if you spent a few minutes to prepare for the worst. Backing up your files is extremely important so that you will always have something to fall back on, but what kind of software do you need to do it? CNet actually has a whole category dedicated to backup software but a lot of it you need to pay for, and I am kinda cheap :D . Instead of adding more clutter to my computer I have decided to use simple, yet powerful, batch files. A batch file is not complicated for those people who aren’t familiar with them. It is simply a text file that has the extension .BAT and inside the file is a series of DOS commands which run one at a time. Batch files are very frequently used by people to perform repetitive tasks which is why they are perfect for scheduling some backups! First, we need to create a blank batch file which is as simple as opening up Notepad, go to File->Save As and type “batch.bat”. Once you press the Save button it will save the file with the .BAT extension instead of the normal .TXT extension. Okay, now hopefully you left the file open after saving it, but if you didn’t all you ever have to do is right-click on the file and select “Edit”. The DOS command for copying a file from one location to another is “xcopy” which is what we are going to use. The command is used in the following format: xcopy "Source" "Destination" /c /q /d /e /h /y For the Source and Destination you just have to put in the folder or file path that you want to backup and the location to backup to. If you have spaces in the path then you will need to include the quotation marks, for example, “C:\Mozilla Firefox\” would need to have the quotes because of the space. So an example command with the path in it would look like this: xcopy "C:\Mozilla Firefox\" "\\Server\C\Firefox\" /c /q /d /e /h /y Now you are probably wondering what the “/c /q /d /e /h /y” does at the end of that command. Each one of those parameters perform a different action. Microsoft has a long list of available parameters that can be added but those are the ones that I use. Here is what each of them does:
  • /c – Ignores all errors which helps make sure that the backup will complete even if there is a small problem.
  • /q – Prevents the command prompt from displaying an “xcopy” message for every file or folder that is copied.
  • /d – Only synchronize Source files if they are newer than the files in the destination. Otherwise every file will get copied each time the batch file is executed.
  • /e – Copy all subdirectories even if they are empty.
  • /h – Copy hidden and system files.
  • /y – Prevents an “Overwrite existing file?” prompt for each file that already exists in the destination.

Feel free to remove any of those parameters that you don’t feel are necessary but those are the ones that I have found to work best for me. Inside the batch file you can add more than one “xcopy” command by separating them by a new line. That means you can backup multiple folders with one single batch file. Once you have the batch file completed and saved you can right-click on it and select “Run” to see if everything works how you want it to. You should probably test it on a small folder with just some test files before you go and do the real thing to prevent any unexpected actions. If everything works great then maybe you want to think about scheduling it to run itself using the Scheduled Tasks located in the Control Panel. Now that wasn’t so bad was it?