41

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.

David Basarab
  • 1,157
  • 5
  • 15
  • 20

7 Answers7

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.

KCD
  • 878
  • 3
  • 11
  • 23
  • 2
    Future versions of windows include RoboCopy. From Vista's XCOPY: "NOTE: Xcopy is now deprecated, please use Robocopy." – George Tsiokos May 18 '09 at 16:28
  • 1
    Interestingly enough, two years later after your comment, George, xcopy still 'rules the world' on Windows 7. –  Aug 25 '11 at 17:22
  • Note that embedded Windows XP environments (POSReady 2009, etc) doesn't ship with xcopy. – Epoc Sep 22 '14 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 '15 at 23:07
  • Don't forget `/J`, which prevents [a bug with XCopy](https://en.wikipedia.org/wiki/XCOPY#Limitation) when copying large files. I'm not sure why they wouldn't make that the default... – BlueRaja Jul 08 '15 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

Dani
  • 1,216
  • 1
  • 13
  • 20
4

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.

2

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
Graeme Perrow
  • 545
  • 1
  • 4
  • 16
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.

Richard
  • 5,309
  • 1
  • 22
  • 20
  • 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. – Kara Marfia May 05 '09 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.

WaldenL
  • 1,248
  • 1
  • 14
  • 26
-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

Jeff
  • 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. – alphamikevictor Jun 10 '15 at 15:33