64

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.

migrated from stackoverflow.com Nov 2 '10 at 3:00

This question came from our site for professional and enthusiast programmers.

  • 1
    try to use xcopy – Sandeep Singh Rawat Nov 1 '10 at 21:53

11 Answers 11

48

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.

  • 2
    The '/T' switch on Xcopy will do a directory & subdirectory structure copy without any files. – mfg Nov 9 '10 at 16:33
  • 2
    Better use robocopy /CREATE /COPYALL /DCOPY:T src dst to just copy the folder and empty files but preserve all timestamps and other attributes. – user1016274 Nov 22 '15 at 21:50
  • 2
    This actually does not provide answer for the question because folder test still nees to be created. – IGRACH Aug 18 '16 at 5:49
  • 2
    @IGRACH I thought md test and then using xcopy was trivially obvious. – vivin Aug 18 '16 at 15:53
  • 1
    Well question is about copy folder not creating new folder and then copying contens in it. – IGRACH Aug 18 '16 at 15:57
32
xcopy c:\test c:\test2\test /s /e /h

Here is info on XCOPY [1,2]

  • 2
    How to stop asking me to input "D"? – YumYumYum Sep 14 '16 at 14:00
  • 2
    @YumYumYum: You can use the /I switch: " /I If destination does not exist and copying more than one file, assumes that destination must be a directory." – Thijs Tijsma Jan 2 '17 at 13:40
17

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.

8

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.

  • 1
    robocopy is a Windows program. The question was about DOS. – grawity Nov 2 '10 at 20:52
  • 8
    @grawity: The likelihood of him actually using 16-bit DOS is very small compared to the high probability of him calling CMD 'DOS', as is common in the real world. – paradroid Nov 3 '10 at 8:31
  • the real question is: is robocopy a Microsoft program? – knocte Feb 26 '18 at 8:17
  • @knocte yes it is – yossico Mar 28 '18 at 15:13
5
XCOPY SourceDrive: DestinationDrive: /S /E

For example, if you need to copy E: drive data to H: drive (external hard drive) through command line or from xboot command environment.

xboot:\>XCOPY E:\ "H:\BackupFolder\" /S /E
3

If the original question is what I have been looking for an answer to then I, and obviously no one above, has come up with a clear solution.

What I am looking for is to copy the test directory into the test2 directory without having to type it in again. Something like

xcopy /isvy c:\test d:\test2

where d:\test2\test does not exist before the copy but does after. This would save on miss-typing test the second time in the destination path. The above command will copy all the files and any directories into test2 but will not create the test directory.

So far

xcopy /isvy c:\test d:\test2\test

is really the only way I have found to get this job done. Again if you have typing issues there is no guarantee that the source and destination directories will match.

An alternative to correct this is

set mydir=test&&xcopy /isvy c:\%mydir% c:\test2\%mydir%

This sets the destination directory in the mydir variable then uses that variable in both the source and destination path. If you type the variable wrong you will either get an error or the destination directory will probably have % at the beginning and end.

It is longer to type but less chance of getting the wrong names. They will be noticeable.

  • It's actually xcopy /ievy c:\test d:\test2\test to keep directory tree structure, and in set mydir=test&xcopy /isvy c:\%mydir% c:\test2\%mydir% only 1 & will suffice, since the result of setting a value is always successful. – NetwOrchestration Aug 18 '16 at 22:00
0
xcopy SWITCHES SOURCE DESTINATION

Where SWITCHES can be:

  • To copy the whole directory structure:

    /hiev

  • To overwrite files in destination (in case destination folder already exists):

    /hievry

  • To overwrite and also copy with ACL+Attribute:

    /hievxok

Most suited to your case:

xcopy /hievry C:\test C:\test2\test

0

I had a similar situation where I needed to copy a number of folders including the folder names to a destination location, and hoped this question marked answered would help, but it truly does not.

Firstly, there are definitely occasions where one would need this ability and I ran into one when I had to copy folders from C:\Windows\Assembly\GAC_MSIL. Windows Explorer refuses to show this folder, so you have to use a command prompt.

If you are familiar with the GAC folder, you would know that the folder names are non-trivial and easy to get wrong if you mistype.

So creating the directory beforehand is not really an option - unless you use a script - which I ended up using, as this was the only real solution.

First dump the folders you want to copy to a temporary file, this is usually based on some pattern e.g.

dir /B policy* > Folders.txt

Then loop over the entries in the dump and copy to destination. Xcopy will take care of creating a folder if you end the destination argument with a backslash (\)

for /F "tokens=*" %%A in (Folders.txt) do xcopy /E /S %%A C:\Dest\%%A\

Put both these commands in a batch file and run.

Now if only xcopy or robocopy has this built in.

0

XCOPY SourceDrive: DestinationDrive: /S /E

  • Welcome to Super User! Can you explain this? (especially the /S /E flags) – bertieb May 15 '18 at 7:38
0

I have come to a functional alternative answer for your question.

Firstly, using FORFILES with /S parameter, confers that every sub-directory in C:\test will be reached.

Secondly, with /C parameter, it is possible to start a chain of commands with native variables, which makes possible to create the identic directories at the destination, and THEN copy the files to inside them, using two conditional structures.

The first conditional structure @isdir==TRUE (make sure to use capital letters), allows to get directories only from source, then creating them at the destiny with MKDIR [path]\@relpath at the end.

Finally, the second one makes sure that it are working with files only with @isdir==FALSE, and then the COPY command simply gets the job done, getting all files reached by FORFILES command, setting [path]@relpath as destination, to copy the files to inside the just created directories.

forfiles /s /c " cmd /c ( if @isdir==TRUE mkdir C:\test\@relpath ) && ( if @isdir==FALSE copy @file C:\test2\@relpath)"

Hope that helps everyone around here.

-2

This will do that from the GUI, don't know how they do it.

"...small program will allow you to select the "from directory" and "into directory", and it will proceed to copy ONLY the directory structure."

http://www.rjlsoftware.com/software/utility/treecopy/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy