Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I copy a file using DOS commands into a directory structure that may not yet exist? I need to be able to force the creation of the directory path to the target file location if that location doesn't already exist.

For example, there is already a file.txt in this location:


And I want to copy it to


but at this time


and all the subdirectories may or may not yet exist.

Basically, I am looking for a "copy and create the target path if necessary" command. What would you recommend is the best way to achieve this?

share|improve this question

migrated from stackoverflow.com Mar 12 '10 at 18:57

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

Is this question actually about DOS (or MS-DOS), or is it about the command line found in NT based Windows versions (2000, XP, Vista, 7, 8, Server)? –  Bob Jun 21 '12 at 13:34
I meant the DOS-like Windows CLI. What do you recommend is the best way to describe "DOS-like Windows CLI" for the post title? –  nodmonkey Jun 24 '12 at 8:11
Generally, I would use Windows command line in the title/body, along with the tags windows and command-line (note that cmd.exe is a tag synonym of command-line). The tag ms-dos should be removed; and the tag dos should not be used at all under any circumstances. –  Bob Jun 24 '12 at 9:13
However, in this case, with the existing/accepted answers being for DOS/MS-DOS, you may as well leave it as-is. For future reference, DOS is/should be interpreted as the operating system(s). While the Windows command line is based on them, there are some notable differences. –  Bob Jun 24 '12 at 9:15

3 Answers 3

up vote 6 down vote accepted

Yeah, that's xcopy. Here's what it'll look like:

xcopy file.txt c:\example\new\path\to\copy\of\file\file.txt

XCOPY info at

You might also want to look into ROBOCOPY, in the XP resource kit and standard in Vista, Windows 7, and Server 2008.

robocopy . c:\example\new\path\to\copy\of\file file.txt
share|improve this answer
xcopy is fine if you don't need to also rename your file. If you do, unfortunately xcopy will ask you whether the destination is a directory or a filename and there is no command-line switch to specify "it's a file". –  romkyns Apr 30 at 1:59

DOS, wow! Anyway you use the XCOPY command.

share|improve this answer

I tried using something like:

xcopy file.txt c:\example\new\path\to\copy\of\file\file.txt

But it would ask me if it was a file or directory. Since I had that in a batch file with 40000 files, it would be impractical. This solution only solves partially my problem: it creates the directory structure, but it requires user interaction. I found the solution to my problem here:


Which is basically to add a "*" at the end of the destination file:

xcopy file.txt c:\example\new\path\to\copy\of\file\file.txt*

EDIT: as pointed by @romkyns, it may have undesired results if you have files that have the same name plus something else (like 'file.txt.bak'). In the same thread posted above, another solution that works is piping "echo f" to your command:

echo f | xcopy file.txt c:\example\new\path\to\copy\of\file\file.txt*

Where you should substitute the "f" for whatever letter your system uses for file, in case you're using it in another language..

share|improve this answer
Which only works if there is no file.txt.bak in the directory, of course. Still, this might work in some scenarios. –  romkyns Apr 29 at 13:29
@romkyns true. a few months after I posted this, I faced myself with the same problem and did the "echo f" solution, it works great as well. Editing my answer... –  msb Apr 29 at 20:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.