42

What is the windows command prompt command to copy files?

I need to move a file from location A to location B. Also if the folder for location B doesn't' exists I want to have it created.

I need this to be a command line so I can automate it.

The version of Windows is XP.

1
  • 1
    robocopy works great too
    – Nixphoe
    Aug 8, 2011 at 1:06

7 Answers 7

53

The command xcopy is what you are looking for. Example:

xcopy source destination /E /C /H /R /K /O /Y

The command above will copy source to destination, files and directories (including empty ones), will not stop on error, will copy hidden and system files, will overwrite read only files, will preserve attributes and ownership/ACL information, and will suppress the prompting for overwrite existing destination files.

/E    Copies directories and subdirectories, including empty ones.
      Same as /S /E. May be used to modify /T.
/C    Continues copying even if errors occur.
/H    Copies hidden and system files also.
/R    Overwrites read-only files.
/K    Copies attributes. Normal Xcopy will reset read-only attributes.
/O    Copies file ownership and ACL information.
/Y    Suppresses prompting to confirm you want to overwrite an
      existing destination file.

For more info type xcopy /? and your command line.

5
  • 2
    Future versions of windows include RoboCopy. From Vista's XCOPY: "NOTE: Xcopy is now deprecated, please use Robocopy." May 18, 2009 at 16:28
  • 1
    Interestingly enough, two years later after your comment, George, xcopy still 'rules the world' on Windows 7.
    – user1797
    Aug 25, 2011 at 17:22
  • Note that embedded Windows XP environments (POSReady 2009, etc) doesn't ship with xcopy.
    – Epoc
    Sep 22, 2014 at 15:54
  • I've suggested an edit to list allllll these options...! If you are not automating it /E /H and maybe /K /O gets you a long way with a few handy prompts
    – KCD
    Jun 25, 2015 at 23:07
  • Don't forget /J, which prevents a bug with XCopy when copying large files. I'm not sure why they wouldn't make that the default...
    – BlueRaja
    Jul 8, 2015 at 10:21
10

Use md to create the folder (it's ok if it already exists)

Use copy or move for files, and xcopy for folders

3
  • Can you give me an example of the syntax? May 5, 2009 at 12:55
  • 3
    copy fred.txt copy_of_fred.txt May 5, 2009 at 13:04
  • 1
    copy /? will give you the help text, sort of like a DOS man page.
    – Scottie T
    May 5, 2009 at 13:34
5

Use ROBOCOPY if you're creating backup scripts. xcopy has been deprecated and will likely be phased out of use in the near future. robocopy can do everything xcopy can. It is also more flexible and reliable. Creating scripts with robocopy will future-proof them.


  1. Use robocopy to easily copy folders. The robocopy command replaces the xcopy command. It can quickly copy entire folders without having to worry about defining the contents. For example, to copy all of the contents of the C:\tools directory to the new folder D:\backup\tools, enter the following:

    robocopy C:\tools D:\backup\tools /e
    

    The /e modifier tells robocopy to include all subdirectories. This includes empty folders. robocopy will automatically copy hidden and system files. It will create new directories if they don't exist at the target location.

  2. Mirror a directory. Mirroring a directory is great for making backups. The mirror option of robocopy will copy all of the contents from the source to the destination. It will then delete anything at the destination that doesn't exist at the source. This ensures that your backup only has the latest versions of your files. For example, to mirror C:\Users\My Documents to D:\backup\My Documents, enter the following:[4]

    robocopy "C:\Users\My Documents" "D:\backup\My Documents" /mir
    

    This function will preserve all permissions of the original files.

  3. Enable restarting. You may want to include the ability to restart the process in case the connection is severed mid-copy.

    robocopy "C:\Users\My Documents" "D:\backup\My Documents" /z
    
  4. Log the copying process. robocopy allows you to create a log file. This can help you pinpoint problems or generate an archive of what's been copied.

    robocopy "C:\Users\My Documents" "D:\backup\My Documents" /log+:<filename>.txt
    

    The /log+ modifier will append the existing log file instead of overwriting it. If you'd prefer to just overwrite the old log file, use /log:.txt.

3

In a batch file:

if not exists locationB\nul mkdir locationB
copy locationA\file locationB

if not exists checks the parameter to see if it exists, but it only works on files. To check for existence of a directory, you need to look for a 'pseudo-file' called "nul" - checking for existence of this file will always return true if the directory exists.

The copy line copies the file called file in directory locationA to locationB and names the file the same thing. If you want to rename the file at the same time, you can do that too:

copy locationA\file locationB\newfilename
2

If you want the ability to synchronise the copy and other advanced features (ignore certain folders, only include certain wildcards) then look at robocopy. Included in Vista and beyond, optional (from resource kit tools) in earlier versions.

1
  • For anything important, I'm going to go with robocopy, as well. I seem to remember a huge performance difference over xcopy when synching a large number of files. May 5, 2009 at 14:03
0

xcopy will create the directory structure for you. Trick is to use the /I option and throw an asterisk at the end of the file name so xcopy thinks you're copying multiple files, otherwise it asks you if the target name is the file name you want, or the directory name you want. For example.

xcopy /I c:\<SourceDir>\<SourceFile> c:\<TargetDirThatDoesNOTExist>

I'd also look at RoboCopy, but you need to get it from the resource kit as it's not in Windows until Vista.

-1

The above command creates an additional directory level with the name of the source file. So... xcopy /I srcdir\dir1\dir2\file1.txt* destdir\dir1\dir2\file1.txt results in destdir\dir1\dir2\file1.txt\file1.txt

1
  • 1
    Does not make sense to reply such an old question, which already has an accepted answer and not contributing some value added on your answer. Jun 10, 2015 at 15:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .