I have noticed, not only in my personal life, but in a majority of my friends and family that things on the computer can get messy, jumbled, and even out of synch.

Take for example my younger brother. He downloads music on a regular basis and plays this music in iTunes. However he has his iTunes music library on a different drive than where his songs are being downloaded. He is constantly in the need of copying files or syncing files from his downloads location to his music library location.

I myself do a lot of webdesign, photography, imaging and ‘tinkering’ in which I like to occasionally backup but without having to manually walk through everything to do so.

My cousin has a jump drive that he keeps a lot of info on which he likes to keep in sync on his jump drive and on his laptop and doesn’t like dragging and dropping, back and forth all the time.

In this post I will show you a few simple lines of a batch file you can create to do all this for you with just a click and a few inputs. Basically it is a syncing file that will do three things:

1. Prompt you whether you want to ‘Backup’ or ‘Synchronize’. The difference here is that when you backup it will only copy those files from the Source directory to the Destination directory. Synchronize will actually copy all those files that are newest in the Source directory to the Destination directory and then anything that is newer in the Destination directory will then be copied back to the Source directory.

2. Prompt you for a Source directory. Keep in mind if using the Backup method this is the directory for the originals.

3. Prompt you for a Destination directory. Keep in mind if using the Synchronization method this is where the backup copies will go.

If you are like many people and don’t really care about the way this works you just want to use it then you can download it here:
Backup & Sychnronize Batch File

So let’s see the code!

Now let’s break it down peice by peice…

The first part of this file is simply header information so when the batch file is executed it displays the message seen in the code above. Nothing special… 🙂

This piece prompts the user using the ECHO command for three variables and sets our variables respectively. The key piece of code is the “@SET /P var=[Prompt text]”. This command will prompt the user in the command windows for whatever is within the [] brackets, then set the result to the “var” you specify. The /P switch is to prompt the user, otherwise it would just set the variable to whatever you put in the brackets and the user would not be prompted.

Also note all the @ symbols before each line. This just tells the command window not to ECHO these lines since they are command lines. The ECHO statement will show it’s ECHO but not the ECHO command itself.

This is two things. The first nested if statement is a check to make sure that the user only chose a response of ‘1’ or ‘2’, anything else is invalid so we go to the Error section of code.

The second is to determine whether the user input a 1 or a 2 and then takes the code to the corresponding section, 1 being for Backup and 2 being Synchronize.

This piece is the section that the code will be taken to if the user chooses a 1 (backup). The GOTO command in the previous chunk of code will take the code to the line labeled with the name called and a colon before it, i.e. “:BackUp”. The core of this script and its functionality is the line “@XCOPY “%_spath%” “%_dpath%” /D /I /S /Y”. This uses the XCOPY command to actually move the files. You can see all the switches and options that can be used with the XCOPY command by typing “xcopy /?” from a command prompt or using the link here:

Once the XCOPY command is complete we issue the PAUSE command which simply stops everything until the user hits a key. This is so the user can review the information in the command window before it closes. After the PAUSE command we issue an EXIT command with a return code of 0 since this tells any other process calling this file that it was a success depending on how that process is coded to handle return codes. It is important you put an exit in after each section of code. In batch files if you don’t it would continue to execute the next line which would start the Synchronization of the folders, which obviously the user doesn’t want if they chose the backup method.

These last lines of code are the same as described for the BackUp section of code except there are different variations of the same commands. Part of the fun of exploring shared code is to examine and interpret the extra code once the core of the code is understood. In that respect there is nothing in this last few lines of code that haven’t been explained in earlier peices of this post, so using that information you should be able to figure out the rest of the code! If you don’t, just let me know and I can help you out.