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

72

18

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.

agezna

Posted 2010-11-01T21:50:02.247

Reputation:

1try to use xcopy – None – 2010-11-01T21:53:08.453

Answers

52

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

Posted 2010-11-01T21:50:02.247

Reputation: 1 001

2Better use robocopy /CREATE /COPYALL /DCOPY:T src dst to just copy the folder and empty files but preserve all timestamps and other attributes. – user1016274 – 2015-11-22T21:50:29.053

2This actually does not provide answer for the question because folder test still nees to be created. – IGRACH – 2016-08-18T05:49:12.383

3@IGRACH I thought md test and then using xcopy was trivially obvious. – vivin – 2016-08-18T15:53:28.377

1Well question is about copy folder not creating new folder and then copying contens in it. – IGRACH – 2016-08-18T15:57:56.340

1@IGRACH rather pedantic distinction, no? – vivin – 2016-08-18T15:58:26.737

1@vivin No that is the point. When you want to copy c:\users\me\New movie that is the best to drive e: you would have to cleate New movie that is the best on drive e: instead just copy whole folder like you can do with windows explorer. – IGRACH – 2016-08-18T16:38:46.547

@IGRACH Ok. But it appears 34 people (not counting myself) seem to think that it is rather obvious one would have to create the destination directory first. I mean, the docs pretty much say that. I've updated the answer either way. – vivin – 2016-08-18T16:40:56.410

1xcopy /s /e /c /h /k /y {source} {destination} – LawrenceC – 2017-07-24T14:02:41.813

2The '/T' switch on Xcopy will do a directory & subdirectory structure copy without any files. – mfg – 2010-11-09T16:33:50.120

RE: /T switch, that can be extremely useful sometimes. Granted, it does not come up often, but when it does, wow does knowing that trick come in handy! :) – Bobson – 2012-03-11T19:59:36.160

36

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

Here is info on XCOPY [1,2]

Zachary

Posted 2010-11-01T21:50:02.247

Reputation:

2How to stop asking me to input "D"? – YumYumYum – 2016-09-14T14:00:31.910

3@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 – 2017-01-02T13:40:25.577

21

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.

Program-Me-Rev

Posted 2010-11-01T21:50:02.247

Reputation: 345

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.

paradroid

Posted 2010-11-01T21:50:02.247

Reputation: 20 970

the real question is: is robocopy a Microsoft program? – knocte – 2018-02-26T08:17:06.683

@knocte yes it is – yossico – 2018-03-28T15:13:45.633

1robocopy is a Windows program. The question was about DOS. – user1686 – 2010-11-02T20:52:07.663

10@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 – 2010-11-03T08:31:34.127

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

Syed khalid Mohammed

Posted 2010-11-01T21:50:02.247

Reputation: 59

2

/S is not required when /E is present. http://www.windows-commandline.com/copy-directory-command-line/

– Giri – 2015-01-15T19:03:57.763

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.

Bill

Posted 2010-11-01T21:50:02.247

Reputation: 41

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 – 2016-08-18T22:00:45.313

1

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

NetwOrchestration

Posted 2010-11-01T21:50:02.247

Reputation: 2 385

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.

mahendrent

Posted 2010-11-01T21:50:02.247

Reputation: 1

0

XCOPY SourceDrive: DestinationDrive: /S /E

Noor All Safaet

Posted 2010-11-01T21:50:02.247

Reputation: 1

Welcome to Super User! Can you explain this? (especially the /S /E flags) – bertieb – 2018-05-15T07:38:22.473

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.

MauGusVicen

Posted 2010-11-01T21:50:02.247

Reputation: 1

-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/

NginUS

Posted 2010-11-01T21:50:02.247

Reputation: 414