XCOPY Switches

I am confused with regard to the switches in xcopy.

I want to copy files from a server to another location.

/s/e[creates subdirectorys and empty subdirectories]
/d [copies files only if the source files are newer than the destination files.

what does /m and /k do?  

I am running the xcopy command as follows and it appears to be copying modified files at the source based on the /d switch.  I want to make sure that I am doing this correctly.  Could I get an explanation on what reseting the archive bit does with /m and /k? Is it resetting a date stamp based on the source file pertaining to when it was last copied or backed up?  Am I way off base?
Who is Participating?

I suppose you are trying to maintain a backup copy.  The method you are using should work, but has the disadvantage that any files deleted from the source directory will not be deleted from the copy.   Also, in general, copying only the changed elements of data should not be your only backup....there is too much danger of some kind of failure of synchronization.

The /M switch is an alternative to using the date to identify files that have to be copied.  Let me explain how you can use it to do weekly full and daily incremental backups.  To do this you would create two batch files, one run weekly and the the run daily.  
The weekly batch file would do a complete new copy in the following steps.
 - rename the existing backup folder to backup.old
 - copy the entire source folder and reset all archive bits (XCOPY /S /E  )
 - delete the backup.old folder
The daily batch file will copy only files with the archive bit (it gets set whenever a file is created or modified)
 - XCOPY /S /E /M

However, if I were you I would use ROBOCOPY instead of XCOPY.  ROBOCOPY has a /MIR option to make a copy of a directory, and will automatically avoid unnecessary copying.  Also, for either XCOPY or ROBOCOPY, you can use an UNC path instead of a drive mapping (e.g. XCOPY \\WORKSTATION\SHARE\FOLDER C:\TEMP\FOLDER)


Harisha M GCommented:
Hi jmorin1,
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]

  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.
               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.

Harisha M GCommented:
    You can see whether the archieve bit is set by using
dir /aa
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Harisha M GCommented:
    In Windows, you can see file attributes by right clicking and going to properties :)
jmorin1Author Commented:
Let me better explain my dillema.  I have 2 remote workstations that I want to move files from.  Let's call them server1 and workstation1.   I want to copy files from the workstation to the server.  I also want to execute this routine without overwriting files that have remained unchanged and new files.  If I am doing this from a workstation unrelated to the two do I have to map a network drive to both server1 and workstation1?  

Can I write a batch job that maps the drive i.e. [net use] and copies the files and disconnects the mapping upon completion?  

Harisha M GCommented:
I don't think you require a mapping in the server.
Just a map of server on workstation is suffecient.

You can write batch file also for that.
If you are planning on running the batch script on an unrelated workstation2 to copy files from workstation1 to server1, you will need to establish drive mappings from workstation2 to workstation1 and from workstation2 to server1.

The batch file would look something like this if you intend to execute it on workstation2:

net use s: \\server1\share\ /u:userdomain\username
net use w: \\workstation1\share\ /u:userdomain\username

xcopy w:\path s:\path /s /e /m

The archive bit is supposed to distinguish file which have been backed-up/archived (archive is clear) and files which are ready to be backed-up/archived (archive bit is set).  The archive bit is turned on if a file has been modified since the last time the bit was cleared.  The /m switch will only copy the files which haven't changed since the last time you ran the batch script.  

Is this what you wanted to do or are you trying to make a new copy of each file without overwriting the files on server1?

Ooops!   Sorry, there is a big error in what I wrote earlier.  XCOPY does not clear the archive bit be default.  It seems that it only clears the archive bit when using the /M option, and that only copies file with the archive bit set on.   This means you *cannot* combine full and differential copies in the way I suggested.  In fact, it makes the /M and /A options pretty useless, IMHOP.

I would go with ROBOCOPY.  I'm not sure if it's included with Win2K3 server, or if you have to install a server resource kit.
You can download it from http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en

I agree with Carlo-Giuliani.  ROBOCOPY would be the better solution.  I am pretty sure that you have to get it from the Resource Kit.  

I didn't realize that you could use UNC paths with xcopy or robocopy.  Looks like I learned something too.  Thanks!
robocopy is a good choice, however it will fail to copy files that you dont have the permission.
I always use robocopy and scopy in conjunction for all my migration projects.
http://www.xxcopy.com/ is a very good solution (for me at least).

And in XCOPY?

net use Y: \\Workstation1\C$
net use Z: \\Server1\C$
XCOPY /D /E /C /I /H /Y Y: Z:
net use Y: /del
net use Z: /del

I think you can do this with UNC naming as well. But I think that would require quotes around the name.

I think if you want to 'mirror' your drives your answer is ROBOCOPY.  You can download Robocopy from the location provided by Carlo.
Simply create 2 shares (source, destination) and a batch file. Schedule your batchfile and ensure ROBOCOPY is reachable (either in same directory or in your path directories such as C:\winnt\system32)

The command you might want to use is:

robocopy \\workstation\share \\server\share /E /Z /MIR /R:10 /W:90 /ETA /LOG+:AppendLog.log

This will MIRROR your files from the Workstation to the Share.  Files deleted on the WS are also being deleted on the Server, it will copy empty and full subdirs, in restartable mode and if the connection drops it will wait 90 seconds before retry. It does that max. 10 times. ETA is the Estimated time of Arrival, can be replaces by /NP (No Progress).  /LOG+: is creating a log and appending to exsting log.  You could make it more dynamic of course by using  a log per day using a variable.

If you do robocopy /??? you will get a very detailed help.  You can for example set Attiributes and check for them, so rather then using mirror you will check for that. Also you can use /SEC to copy the security information (both source / dest must be NTFS).

Let me know your results.

Harisha M GCommented:
split, based on best answers according to page expert?  As in my opinion posting a xcopy /? output is not very helpful if the person asks for clarification.
BTW. The newest version of robocopy is found in the Windows 2003 RK-Tools Kit.
It's available on: http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&DisplayLang=en

This version is (for as far as I've tested) running under Windows 2000, but has some new options which might be useful.

One thing: I could't extract the rktools pack completely under W2K, so I used a WXP machine for that. After that, no problem on using the Tools.
OOPS...location already posted...SMART.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.