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 Trott | Windows Tips and Tricks | April 22, 2009
1,001 words, estimated reading time 4 minutes.

Backing up your data is vital, whether it's your holiday photos or your business, and there are many products out there that all 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 doesn't it? But don't worry, I'll show you how to do a differential backup or deploy a website solution. The technique is the same; in fact, 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 the message "Does z:\inetpub\wwwroot\mywebsite specify a file name or directory name on the target?". This is because 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 and good, 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 entire 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." What we need to do now is tell the command to overwrite existing files, so add /Y and /R to the command.

There are a few other switches that we are going to 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 also 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) all you need to do is specify *.jpg instead of the *.* source pattern.

Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

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 comment(s). Why not join the discussion!

We respect your privacy, and will not make your email public. Learn how your comment data is processed.

  1. JD

    On Thursday 29th of September 2011, John Doe said

    Is there a way using copy or xcopy to make copies of only selected files, e.g.: xcopy (file1.dat,file2.exe,file3.doc,file4.dat) .destinationSubdirectory ??? I've been trying to figure this out for quite some time, but with no success.

  2. MC

    On Friday 14th of May 2010, mcook said

    When trying to copy across folders that already exist in the destination directory, I recieve a "File creation error - The directory is not empty." error, despite using the /y and /c switches.

    Ie: xcopy c:photos-temp*.* x: /C/E/H/R/K/D/Y/F

    What should I be doing differently?

    1. CH

      On Friday 30th of July 2010, chris replied

      Did you get an answer to this? I'm having the
      same problem.

      1. JE

        On Wednesday 27th of October 2010, Jeremy replied

        I am having the same problem, havent found a solution.

  3. JO

    On Friday 26th of March 2010, Johan said

    Hi guys,

    I am looking for a command that can Search for a *.pst (dir /s *.pst) than copy all the *.pst's to a server "serverpstbackupusername"

    can anyone help me with this?

  4. EE

    On Wednesday 30th of December 2009, EricE said

    Yes thank you for these posts it is useful information.
    My question is this:
    I am using xcopy with the /T option to only copy directory/folder structure from one location to another without the files. My only problem is that I only want the first level of sub directories from the source location and not the lower ones. Is it possible to omit lower level subs, or subs in general? or even specify which levels you want. I have read about the /Exclude option but from what i've read it will not do the job i am looking for.
    TY

  5. BM

    On Saturday 9th of May 2009, Bruce M. said

    Hello! This is great information. However, I suggest you all look into a 3rd party replacement for windows standard "xcopy" command. The tool I use is "XXCOPY" from www.xxcopy.com. This is one awesome utility! It has over 200 command switches, allows remote configureation files, knows how to test for changed files, works over a network and on and on. It's available in shareware for personal use or for about $40 for professional use. I use it to back up my home and office drives. Check it out! - Bruce

  6. BR

    On Monday 23rd of February 2009, Bill Rod said

    xcopy c:util*.* h:util /s/v/m/f/i/y
    xcopy c:docs*.* h:docs /s/v/m/f/i/y

    You can put as many lines like these in your batch file and have them all do something different.
    That has worked for me for the past zillion years. I dont like operating systems that do not let you access DOS because this works so well and batch files are very useful.
    If you have a lot of files with different extensions in one folder you can sort them on your backup like this.

    xcopy c:util*.doc H:docs /s/v/m/f/i/y
    xcopy c:util*.txt h:textfiles /s/v/m/f/y
    xcopy c:util*.xls h:exce /s/v/m/f/y

    You can also copy all your directories into on directory on your backup although I dont know why you would want to.

    /S copies subdirectories
    /V verifies files which is probably unnecessary but does not slow things much.
    /M copies only new or modified files and turns the archive bit off.
    /F Displays source and destination while copying. It gives you something to watch and you will see what if going on.
    /Y Over writes files without proumpting your for a Y/N

    I put all these lines of text in a file called backup.bat
    You really need the /M switch espically if you have USB 1.1 as the first time you run the backup file it can take forever depending on the files and number of directories you are backing up. The second time it will only copy files that have been modified or are new.
    Another good feature of this batch system is that all your files or programs are not compressed and you can actually run them from the backup disk as if they were on the C: drive
    Also, the first time you run this batch it may ask you if you are creating a file or a directory. You will click on directory
    Enjoy.
    Bill Rod

  7. PM

    On Monday 14th of July 2008, Peter Monahan said

    Thanks for the step by step on xcopy located here:

    https://timtrott.co.uk/xcopy-tool/

    I need to copy all of the directories and folders from my P: Drive to a directory on my T: Drive AND then I would like to be able to run the CMD line again in the future, only replacing modified files. The problem I am having is that by using the destination T:Backup or T:Backup only works if the destination folder does not already exist. After the first XCOPY, the CMD fails because the directory already exists.

    Ideas would be appreciated.

    Thanks in advance.
    Peter

    1. GI

      On Friday 26th of August 2011, Giri replied

      you can use /y switch to override the existing files. Check this link :<a href=http://www.windows-commandline.com/2011/02/xcopy-command-syntax-examples.html>Xcopy command examples</a>

  8. AR

    On Saturday 24th of May 2008, Arun said

    The differential backup tutorial is awesome... But is it possible to modify this so that missing files in the source are deleted in the destination too? (Similar to a Mirroring software)

  9. FR

    On Friday 16th of May 2008, Francois said

    Quotes
    "c:my foldereven very longnames"

    thx wolf, was looking 4 differential copy with xcopy

  10. MA

    On Sunday 27th of April 2008, Marcos said

    First, thanks for this useful backup proposition.

    I noticed the following problem with the solution proposed: suppose that you made your backup and then, you deleted a file in the backup folder. If you do the backup again, this file will not be copied again. The solution to that is to omit the option /M.

    1. BR

      On Monday 23rd of February 2009, Bill Rod replied

      If you eliminate the M option if will not turn off your arcive bit in the original drive and it will copy all the files over again even if they have not be modified or new. That could take forever instead of a few seconds

  11. MA

    On Sunday 27th of April 2008, Marcos said

    Reply to Jim
    ---------------

    I propose the following command if you want to copy files of a given extention into one single folder:

    for /r SOURCE_PATH %i in (*.EXT) do xcopy "%i" DEST_PATH /C/H/R/K/D/Y

    where:
    - SOURCE_PATH is the root of the folders you want to search
    - EXT is the file extension

    I hope this answers Jim's question

  12. JI

    On Tuesday 1st of April 2008, Jim said

    Thank you for making this info available. I have been trying to locate a program with which I could back up data at work and this one fills the bill.

    I do have one question though: In addition to using xcopy for specific folders and subdirectories, I would like to be able to copy all of my .doc and .xls files to a single destination folder WITHOUT subfolders/subdirectories. That is, I want to copy alll of those files from my C and D drives to a single folder on my backup drive. I have tried doing this a number of times but still cannot get it right. Can you suggest a command line that will execute this for me? Thanks again.

    Jim

  13. AN

    On Thursday 28th of February 2008, ankur said

    hello sir please tell how can use xcopy to find a latest file among set of files and then copy it.

  14. JO

    On Saturday 9th of February 2008, John said

    This is great.

    how do you use this command line in my batch file without the ~1?

    c:Docume~1JohnMyDocu~1*.*

    1. Tim Trott

      On Sunday 10th of February 2008, Tim Trott  Post Author replied

      You can put the full path in double quotes, e.g.

      "c:Documents and SettingsJohnMy Documents*.*"

      Hope that helps.