Thursday, April 2, 2009

Command line options for XCOPY

Many people are not aware of some additional command line switches for most if not all dos commands.  In this article I will briefly try to explain one or more of XCOPY 's switches.

I have used the following options to sync up an FTP directory structure between two ftp servers without copying the data files themselves. This command will only copy the folder structure including empty folders from the source to the destination folder.

xcopy /T /E  c:/source_folder c:/destination_folder

If you have used other xcopy switches please drop me a line.

For more of my command line articles please click here.

For reference I have copied some additional info below.

  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 the size of 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.
  /B           Copies the Symbolic Link itself versus the target of the link

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

 

5 comments:

henry said...

tried this item and the utility reports 'invalid switch - /d:'

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

i was looking for a way to pass the current date to the scheduled task in windows xp

Scott Mckeown said...

Did you try to add the date after the /d: switch? Something like

xcopy /d:1-1-09 sourcefilename destfilename

I beleive the /d: needs a date to function properly.

henry said...

i figured out what i was doing wrong. my command line switch was /d: and should have been /d . then it does work the way i expected.
now i'm trying to figure out how to calculate the date parameter in a batch file so that i can run from windows xp task scheduler. anybody know how to do that ?

Scott Mckeown said...

See if this helps

FOR /F “TOKENS=1* DELIMS= ” %%A IN (’DATE/T’) DO SET CDATE=%%B
FOR /F “TOKENS=1,2 eol=/ DELIMS=/ ” %%A IN (’DATE/T’) DO SET mm=%%B
FOR /F “TOKENS=1,2 DELIMS=/ eol=/” %%A IN (’echo %CDATE%’) DO SET dd=%%B
FOR /F “TOKENS=2,3 DELIMS=/ ” %%A IN (’echo %CDATE%’) DO SET yyyy=%%B
SET date=%mm%%dd%%yyyy%

Then you can use the %date variable in your script.

Scott

Anonymous said...

emm. attractive thread!