What is the windows command line command to copy files?

221,201

Solution 1

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.

Solution 2

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

Use copy or move for files, and xcopy for folders

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
221,201

Related videos on Youtube

David Basarab
Author by

David Basarab

David Basarab Software craftsman that is constantly scrubbing my code.

Updated on September 17, 2022

Comments

  • David Basarab
    David Basarab over 1 year

    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.

    • Nixphoe
      Nixphoe almost 13 years
      robocopy works great too
  • David Basarab
    David Basarab about 15 years
    Can you give me an example of the syntax?
  • Adam Gibbins
    Adam Gibbins about 15 years
    copy fred.txt copy_of_fred.txt
  • Scottie T
    Scottie T about 15 years
    copy /? will give you the help text, sort of like a DOS man page.
  • Kara Marfia
    Kara Marfia about 15 years
    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.
  • George Tsiokos
    George Tsiokos about 15 years
    Future versions of windows include RoboCopy. From Vista's XCOPY: "NOTE: Xcopy is now deprecated, please use Robocopy."
  • Admin
    Admin almost 13 years
    Interestingly enough, two years later after your comment, George, xcopy still 'rules the world' on Windows 7.
  • Epoc
    Epoc over 9 years
    Note that embedded Windows XP environments (POSReady 2009, etc) doesn't ship with xcopy.
  • alphamikevictor
    alphamikevictor almost 9 years
    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.
  • KCD
    KCD almost 9 years
    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
  • BlueRaja
    BlueRaja almost 9 years
    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...