DOS Backup Script

There are many big, automated backup software available. Some cost you money and others are free. Some flavors of Windows even come with a Backup program. However using Widows' built-in DOS commands to automate a custom backup function is a quick-and-dirty way of automating your backup. Your system's built-it DOS commands use up very little processing overhead compared to over-the-shelf software.

Here is how to do it:

    • First think for a minute... what do you want to back up? For most Windows users, important things to back up are your Documents folder and your the Desktop screen.

    • Each of the important items that was just mentioned are stored on your hard drive and have a unique folder path associated with them. Here are the folder paths for the items mentioned:

        • Windows Vista and Windows 7 and later:

          • C:\Users\[username]\Documents

          • C:\Users\[username]\Desktop

      • Windows XP users:

        • C:\Documents and Settings\[username]\My Documents

        • C:\Documents and Settings\[username]\Desktop

    • Referring back to the DOS command line at the top of this page, there are three parameters (enclosed in square brackets) that must be modified before the DOS command is usable:

      • Source Path - the source folder path

      • Dest Path - the destination folder path

      • Log Path - the log file path

    • The 4 different folder paths detailed above (2 for WinXP and 2 for newer Windows) are Source Folder Paths. These folder paths tell the system where on your hard drive to look in locating the files you want to back up.

    • Once the system knows WHAT to backup via the Source Folder Paths, the system then must know WHERE to place the backups of those files. The Destination Folder Paths tell the system where to put the backups.

      • For example, let's say you have a USB flash drive inserted into your computer and you know it is designated by the drive letter E.

      • Then within the flash drive you created a folder called "backups" and want our DOS command to save the backups within this Backups folder.

      • Your Destination Folder Path in this case would be "E:\backups".

    • As the DOS command performs the backup, it will generate a log that details the individual files that are backed up, and if any errors arise during the operation. The Log Path describes where this log file will be stored.

      • For example, we'll store the log file in the Backups folder within the flash drive.

      • The Log Path would be "E:\backups".

    • Here's how to modify the DOS command line:

      • Open up a Notepad window

        • Windows Vista or later: Click Start button, type "notepad" in bottom search bar, press ENTER key

        • Windows XP users: Click Start Menu, Click RUN option, type "notepad", then click OK button

      • Highlight the code below (the entire line), and press CTRL+C on keyboard to copy into memory.

xcopy /d/e/c/i/y "[Source Path]\*.*" "[Dest Path]\" >> "[Log Path]\log-%DATE:/=%.txt"

      • Click within the blank Notepad window and press CTRL+V and the code should appear

      • Press the ENTER key on your keyboard to go to the next line down and press CTRL+V again.

      • Your Notepad window should now look like this:

xcopy /d/e/c/i/y "[Source Path]\*.*" "[Dest Path]\" >> "[Log Path]\log-%DATE:/=%.txt"

xcopy /d/e/c/i/y "[Source Path]\*.*" "[Dest Path]\" >> "[Log Path]\log-%DATE:/=%.txt"

      • We copied the DOS command line 2 times because we have 2 folders/locations we wish to back up - the My Documents location and the Desktop location.

      • Now we need to replace the three parameters - Source Path, Dest Path, and Log Path - within each of the two command lines with folder paths corresponding to the folder locations.

      • Let's say we're using a Windows 7 system. The result looks like this

xcopy /d/e/c/i/y "C:\Users\[username]\Documents\*.*" "E:\backups\" >> "E:\backups\log-%DATE:/=%.txt"

xcopy /d/e/c/i/y "C:\Users\[username]\Desktop\*.*" "E:\backups\" >> "E:\backups\log-%DATE:/=%.txt"

    • Now you need to replaced the [username] portion with your actual Windows username. Let's say your Windows username is "Pat". The resulting command lines should now look like this:

xcopy /d/e/c/i/y "C:\Users\Pat\Documents\*.*" "E:\backups\" >> "E:\backups\log-%DATE:/=%.txt"

xcopy /d/e/c/i/y "C:\Users\Pat\Desktop\*.*" "E:\backups\" >> "E:\backups\log-%DATE:/=%.txt"

    • Now save the Notepad file and call it something like "backup.cmd".

    • IMPORTANT: During the process of saving the file, be sure that when you enter the name "backup.cmd" as the filename, that you DO include the double quotes.

      • By omitting the double quotes, the system will save your new DOS command file as just a simple text file (TXT format). We want to ensure the file is instead saved in the .CMD file format... the Command file format.

      • The result file icon for your new "backup.cmd" file should look like a white box with a little gear inside.

  • That's it. Now just double-click on your new command file will come alive. Here is a play-by-play:

      • Your system executes the first command line:

        • It dives into your Documents folder specified by "C:\Users\Pat\Documents" and selects all files and sub folders contained in there.

        • It then dumps a copy of those files into your flash drive specified in "E:\backups\".

        • All the while it generates a log file that details every file it encounters and saves the log as a simple text file (TXT format) and places it in "E:\backups\". The log's file name has the date appended to it.

      • Your system completes execution of the first command line, then executes the 2nd line, and the similar process repeats.

    • Now just double-click on your backup.cmd icon whenever you want to run a backup again. You can edit your backup.cmd file to include more command lines (for additional folder locations) later on, and you can include the backup.cmd file into the Windows Task Scheduler to fully automate your backups for daily or weekly execution.

-Jason Lim, March 8, 2011