102

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.

12 Answers12

72

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.

vivin
  • 1,603
48
xcopy c:\test c:\test2\test /s /e /h

Here is info on XCOPY [1,2]

Hastur
  • 19,483
  • 9
  • 55
  • 99
39

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.

ebyrob
  • 218
10

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.

paradroid
  • 23,297
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
Malachi
  • 1,063
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.

Burgi
  • 6,768
Bill
  • 41
  • 1
2
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

2

I managed to achieve this by using PowerShell Copy-Item function

Copy-Item 'C:/test' 'C:/test2' -Recurse -Force

It will copy all files and folders while preserving folder hierarchy. -Force overwrites the destination if already exists.

Greenonline
  • 2,390
farooq
  • 159
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

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.

-3

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/

NginUS
  • 410