In-store Clinics
Using XCOPY to Back Up Files and Settings

One method of backing up your files to another location under Windows can be accomplished using the XCOPY program in a Command Prompt window. You can also build a text batch file that will execute one or more XCOPY commands in a sequence and then configure a scheduled event to execute your file at a specified time or interval. A batch file is nothing more than a text file with a series of command line strings listed in the order of execution. To avoid entering commands that are invalid or might generate errors, you can always enter the command manually in a command window, and then copy it into the text file.

To create a batch file script that copies a group of common files and settings to another drive or network share, you will need to identify what directories or files you want to duplicate. To archive most of the normal document and media files, favorites, and desktop configuration of the different users, you will want to duplicate at least the following three locations:

C:\Documents and Settings\All Users
C:\Documents and Settings\<user name>
C:\Windows\Profiles (contains legacy user profiles and Outlook Express data.)

Add to these any other files or directories where important data is being saved, and keep in mind that the locations specified here already include "My Documents" and all the other sub folders under it, including "My Music" "My, Pictures", etc. If you are running applications that store their data somewhere else, you need to determine the file path being used. Program files rarely benefit from being backed up, but since "legacy" settings and scripts may be saved to the application directory, building an archive with these program directories makes sense. You would still have to reinstall the application to build the necessary registry changes and to install the program libraries, but once your archived directory is copied over the top of the fresh install, all of your settings should be restored in most cases. By using the complete path in the command, the batch file should not need to be running from the root directory, and you can specify multiple paths or drive letters in the same script.

Using the directory examples listed above, an XCOPY backup batch file might look something like this:

xcopy "C:\Documents and Settings\All Users" "d:\backup location\All Users" /r /e /h /k /c /y
xcopy "C:\Documents and Settings\john smith" "d:\backup location\john smith" /r /e /h /k /c /y
xcopy "C:\Documents and Settings\jill smith" "d:\backup location\jill smith" /r /e /h /k /c /y
xcopy "C:\Windows\Profiles" "d:\backup location\Profiles" /r /e /h /k /c /y

xcopy "C:\Program Files\Procomm Plus" "d:\backup location\Procomm" /r /e /h /k /c /y

The first time these commands are run, if the target directory structure does not exist, you should receive a prompt asking something like:

"Does D:\backup location\Procomm specify a file name or directory name on the target (F = file, D = directory)?"

Be sure to answer "Directory" or else you will end up with a really big file containing all of the content of the original files, and no way to split-out or identify anything useful in it. You can always start off by drag-and-drop copying the directories to the target drive from within Windows to avoid this, but if you receive an error, the copy process will terminate. Note the use of quote (") marks around the source and target paths; these are necessary because of the spaces included in the long filename path. The command line toggles I have selected here will make sure that: (/r) Read-only files are over-written, (/e) Empty directories and subdirectories are duplicated, (/h) hidden files are copied, (/k) File attributes are copied, (/c) xcopy will continue to execute even if errors are encountered, and (/y) xcopy will not pause to prompt for your decisions.

Build your batch file:
This can be done several ways, although I usually just use the right click method. To do this:

1. Open "My Computer" or some other location where you want to store the batch file.

2. Right click and select "New", "Text Document". A new_text_document.txt file will be created and is selected for renaming.

3. Enter a name for your file and change the extension from .txt to .bat, such as "archive.bat"

4.You should receive a warning that changing the extension could make the file unusable. Click "Yes" to confirm the change.

5. Right click on the file and select "Edit". The empty file should be opened in NotePad.

6. Open a command prompt window for testing your xcopy commands. Click Start, Run, enter "CMD" in the open box (without the quotes) and click "OK".

7. Build your xcopy commands in the NotePad document. To do this using one of the following methods:

One transfer method is to enter the xcopy command and verify that it works in a command prompt window. Once the copy process is complete, then press F3 to retype the last command or press the up arrow. Right click in the window and select "Mark" then left click and drag to highlight the command you previously entered. Press the "Enter" key once the text has been selected to copy it to the Windows clipboard. Click in your NotePad window and paste the text. You may need to remove the hard return if your command wrapped to the next line, and remove the C:\> prompt from the beginning.

A variation of this is to type the command in the NotePad window first. Select the text in the window and press control-C or use the Edit, Copy menu to transfer it to the Windows Clipboard. Click to select the CMD window and then right click and select "Paste" to enter the command at the dos prompt. If you copied the entire line, this probably included a return that started the command, otherwise, press "Enter" to start.

8. Save the file once all your lines have been created.

XCOPY Notes:

To execute the batch file, just double click on the icon. You can create shortcuts to your start menu, desktop, or any other location you find convenient.

To add this to the Windows Task Scheduler so that it runs automatically, open Control Panel and then open the "Scheduled Tasks" tool (classic view; under task view, select "Performance and Maintenance" and then click on "Scheduled Tasks" at the bottom of the window.) Double click on "Add a Task" and follow the prompts to configure when to run the batch file. To do this:

  1. Click or select "Add a Task" to start the wizard. Click Next.
  2. Click Browse and navigate to the location of your batch file. Select the file and click "Open"
  3. Enter a name for the task, Select when to perform the task (Daily, Weekly, Monthly, One time only, When my computer starts, When I log on) and click Next.
  4. Enter the time to start the task and other options, and then click Next.
  5. Enter the user name and password that will be used to execute the batch file. This is important if you have multiple users on a single computer, and should be a user with an administrator account. Limited access accounts do not have all of the necessary security and access rights to be able to open or copy many of the files described in this archive process.
  6. Click to "Open advanced properties..." if you want to specify a starting directory, make changes to the schedule, etc. You can alter these settings at any time in the Scheduled Task window by right-clicking and selecting "Properties" for the task.
  7. Click Finish to save the task. Remember, your computer must be turned on and Task Scheduler active for the batch file to run as scheduled.
  8. To restore files that have been "backed up", simply duplicate your batch file and reverse the source and target locations.

Reduce the time it takes to copy files by adding a "/M" option to the lines to copy only files that have been modified since the last copy. Note that if you use programs that clear the "archive" file attribute, it is possible that xcopy could miss a file now and again.

To run the xcopy batch file in a minimized window, create a shortcut to the batch file; right click the shortcut and select properties; on the "Shortcut" tab, change the "Run" option from "Normal Window" to "Minimized" and click OK to save the change. If you are using Task Scheduler to run the file, browse to the shortcut instead of the batch file itself.

Entering " XCOPY /? " at a command prompt will display the help screen for using XCOPY to replicate files and directory structure. The syntax and possible command line switches for XCOPY are shown here:

XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W] [/C] [/I] [/Q] [/F] [/L] [/G] [/H] [/R] [/T] [/U] [/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z] [/EXCLUDE:file1[+file2][+file3]...]

source    Specifies the file(s) to copy.
destination        Specifies the location and/or name of new files.
/A   Copies only files with the archive attribute set, doesn't change the attribute.
/M   Copies only files with the archive attribute set, turns off the archive attribute.
/D:m-d-y  Copies files changed on or after the specified date. If no date is given, copies only those files whose source time is newer than the destination time.
/EXCLUDE:file1[+file2][+file3]... Specifies a list of files containing strings.  Each string should be in a separate line in the files.  When any of the strings match any part of the absolute path of the file to be copied, that file will be excluded from being copied.  For example, specifying a string like \obj\ or .obj will exclude all files underneath the directory obj or all files with the .obj extension respectively.
/P   Prompts you before creating each destination file.
/S   Copies directories and subdirectories except empty ones.
/E   Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.
/V   Verifies each new file.
/W   Prompts you to press a key before copying.
/C   Continues copying even if errors occur.
/I   If destination does not exist and copying more than one file, assumes that destination must be a directory.
/Q   Does not display file names while copying.
/F   Displays full source and destination file names while copying.
/L   Displays files that would be copied.
/G   Allows the copying of encrypted files to destination that does not support encryption.
/H   Copies hidden and system files also.
/R   Overwrites read-only files.
/T   Creates directory structure, but does not copy files. Does not include empty directories or subdirectories. /T /E includes empty directories and subdirectories.
/U   Copies only files that already exist in destination.
/K   Copies attributes. Normal Xcopy will reset read-only attributes.
/N   Copies using the generated short names.
/O   Copies file ownership and ACL information.
/X   Copies file audit settings (implies /O).
/Y   Suppresses prompting to confirm you want to overwrite an existing destination file.
/-Y  Causes prompting to confirm you want to overwrite an existing destination file.
/Z   Copies networked files in restartable mode.

The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.

Understanding Tech

Get In-store Clinic Update

Print this article

Shop Online


Your Name:

Your E-mail:

Your Friend's Name:

Your Friend's E-mail:

© Micro Electronics, Inc.