Commmand line command to copy entire directory (including directory folder) to another directory

using the command line, I'd like to copy one directory to another. For example there is directory C:/test and C:/test2.

I'd like to copy C:/test into C:/test2 so that the result will be C:/test2/test

Everything I've found so far will only copy the files and folders contained in C:/test into C:/test2, but leaves out the parent directory.


Try using XCOPY with the /E switch. More info here.

I haven't had to access this information from my brain in years!

UPDATE

The documentation says that it copies all files and subdirectories from the source directory (meaning that the parent directory is not created), so you would have to create test in C:\test2 first and then use XCOPY.


xcopy c:\test c:\test2\test /s /e /h

Here is info on XCOPY [1,2]


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.


I recommend robocopy over xcopy, as it has a lot more options, including keeping timestamps intact, which I find essential.

Robocopy needs to be added on XP/2003, but it is standard from Vista onwards.

I actually usually use xxcopy, but the 64-bit version is not free.