Windows XCopy - How to Easily Backup Folders and Subfolders

Backing up your data is vital, whether it's your holiday photos or your business. Here's how to use XCopy to easily backup data.

By Tim TrottWindows Tips and Tricks • April 22, 2009
937 words, estimated reading time 3 minutes.
Windows XCopy - How to Easily Backup Folders and Subfolders

Backing up your data is vital, whether it's your holiday photos or your business, and there are many products out there that do the same thing: backup your data. XCopy can also be used as a quick deployment solution.

XCopy Parameters and Switches

First, let's bring up the command prompt and look at xcopy.

C:\Documents and Settings\....\>xcopy /?
Copies files and directory trees.

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.

C:\Documents and Settings\>_

Looks pretty intimidating. But don't worry; I'll show you how to do a differential backup or deploy a website solution. The technique is the same; the only difference is the target server.

Backing up Data with XCopy

For this example, we will assume that my documents are located in C:/My Documents/ and that I want to copy them to another drive (which can be a mapped network drive)

xcopy "c:\my documents\mywebsite*.*" "z:\inetpub\wwwroot\mywebsite"

When you run this command, you will get "Does z:\inetpub\wwwroot\mywebsite specify a file name or directory name on the target?". The command can't tell if you want to copy to a folder or a file called my backup. This can be remedied using the /I switch to force the destination as a folder.

This will now copy all the files in c:\my documents\mywebsite\ to z:\inetpub\wwwroot\mywebsite\. All is well, but this will not copy sub-directories. To do this, we need to add /E to the end of the command. It would also be a good idea to verify the data we are copying to be sure it hasn't been corrupted by adding /V to the command. We can then put this command into a batch file that we can run to create a simple deployment solution.

Create a new batch file and enter in our new command:

xcopy "c:\my documents\mywebsite\*.*" "z:\inetpub\wwwroot\mywebsite\" /I/E/V

When this is run, it will copy the contents of c:\my documents\mywebsite\ to z:\inetpub\wwwroot\mywebsite\, including all subdirectories. When run again, however, you will get a message stating "File creation error - Cannot create a file when that file already exists." We need to tell the command to overwrite existing files, so add /Y and /R to the command.

There are a few other switches that we will use, /C will continue even if there is an error. This prevents the backups from halting altogether. /H will copy any hidden and system files, such as thumbs.db (Windows Explorer Thumbnails), /K will copy over file attributes such as Read-Only, Hidden, etc. /D and /M will copy files if they have been modified since the last backup.

Our command should now look like this:

Differential Backup using XCopy

xcopy "c:\my documents\mywebsite\*.*" "z:\inetpub\wwwroot\mywebsite\" /C/E/H/R/K/D/M/Y

What we have here is a differential backup batch file using xcopy.

You can add multiple lines to the batch file to deploy or backup more than one folder.

If you only wish to update/copy/backup/deploy a particular file extension (e.g., all the jpeg images), specify *.jpg instead of *.* in the source pattern.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

This post has 19 comments. Why not join the discussion!

New comments for this post are currently closed.