Lonewolf Online

The website of an amateur photographer, astronomer and petrolhead.

XCopy Command

This built in dos command will allow you to create differential backups, copy files and archive documents.

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.

For the average home user an expensive backup solution is not the best solution, or due to budget requirements it may not be possible to purchase software. But did you know that Windows comes with its own software?

In this article I will show you how to use the command line tool XCOPY to create backups of your data and to schedule the task to run every day.

First, lets 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? For this example we will assume that my documents are located in C:/My Documents/ and that I want to back them up to another drive.

xcopy "c:\my documents\*.*" "j:\my backups\"

When you run this command you will get the message “Does J:\my backups\ 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\ to J:\My Backups\. All 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 coping 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 backup

Create a new text file on the desktop and call it “backup.bat”. Open it in a text editor (right-click then Edit should work).

Add in our new command:

xcopy "c:\my documents\*.*" "j:\my backups\" /I/E/V

When run, this batch file will copy the entire contents of C:\My Documents\ to J:\My Backups\ including sub directories. 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 id there is an error. This prevents the backups from halting all together. /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:

xcopy "c:\my documents\*.*" "j:\my backups\" /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 backup more than one folder.

Here is my backup batch file, the first line will close Microsoft Outlook before backing up my emails.

taskkill /f /im outlook.exe
xcopy "f:\my work\*.*" "j:\my work\" /c/e/h/r/k/d/m/y
xcopy "C:\Documents and Settings\...\Application Data\Thunderbird\Profiles\*.*" "j:\thunderbird\" /c/e/h/r/k/d/m/y
xcopy "C:\Documents and Settings\...\Local Settings\Application Data\Microsoft\Outlook\*.*" "j:\outlook\" /c/e/h/r/k/d/m/y
xcopy "C:\Documents and Settings\...\Application Data\Microsoft\Outlook\*.*" "j:\outlook\" /c/e/h/r/k/d/m/y
xcopy "d:\websites\*.*" "j:\websites\" /c/e/h/r/k/d/m/y
xcopy "d:\My Programming\*.*" "j:\My Programming\" /c/e/h/r/k/d/m/y
xcopy "d:\My Pictures\*.*" "j:\My Pictures\" /c/e/h/r/k/d/m/y

 

Share/Save/Bookmark

 

What Others Are Saying

Peter Monahan Posted on Monday 14th July 2008 at 9:18am Comment

Thanks for the step by step on xcopy located here:
http://lonewolf-online.net/computers/kb/windows/xcopy-command/

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

Arun Posted on Saturday 24th May 2008 at 7:13am Comment

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)

Francois Posted on Friday 16th May 2008 at 11:30am Comment

Quotes
“c:\my folder\even very longnames\”

thx wolf, was looking 4 differential copy with xcopy

Marcos Posted on Sunday 27th April 2008 at 11:32am Comment

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

Marcos Posted on Sunday 27th April 2008 at 10:19am Comment

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.

Jim Posted on Tuesday 1st April 2008 at 12:22pm Comment

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

ankur Posted on Thursday 28th February 2008 at 1:05am Comment

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

Gravatars Lonewolf Posted on Sunday 10th February 2008 at 9:46am Author

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

“c:\Documents and Settings\John\My Documents\*.*”

Hope that helps.

John Posted on Saturday 9th February 2008 at 6:02pm Comment

This is great.

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

c:\Docume~1\John\MyDocu~1\*.*

 

Have Your Say

 

 

 

 

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment. The authors reserve the right not to publish any comments that they believe are hateful, racist, demeaning or otherwise inappropriate.

    www.flickr.com
    This is a Flickr badge showing public photos and videos from Tim Trott. Make your own badge here.
  • Recent Additions
  • Search




  • Page copy protected against web site content infringement by Copyscape
This page was last updated on Monday, July 28th, 2008.
Unless otherwise stated, all photographs and content Copyright © 2000 - 2008 Tim Trott, All Rights Reserved. No graphics, photographs or content may be used without written permission.
Website Design and Graphics Copyright © 2005-2007 Tim Trott. home :: sitemap :: disclaimer:: contact