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.

link|improve this question

robocopy works great too – Nixphoe Aug 8 '11 at 1:06
feedback

5 Answers

up vote 11 down vote accepted

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.

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

link|improve this answer
This worked perfectly. Thanks. – David Basarab May 5 '09 at 13:42
1  
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
Interestingly enough, two years later after your comment, George, xcopy still 'rules the world' on Windows 7. – David Collantes Aug 25 '11 at 17:22
feedback

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

Use copy or move for files, and xcopy for folders

link|improve this answer
Can you give me an example of the syntax? – David Basarab May 5 '09 at 12:55
copy fred.txt copy_of_fred.txt – Adam Gibbins May 5 '09 at 13:04
copy /? will give you the help text, sort of like a DOS man page. – Scottie T May 5 '09 at 13:34
feedback

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.

link|improve this answer
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 5 '09 at 14:03
feedback

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
link|improve this answer
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.