55

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:

C:\file.txt

And I want to copy it to

C:\example\new\path\to\copy\of\file\file.txt

but at this time

C:\example\

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?

Oliver Salzburg
  • 86,445
  • 63
  • 260
  • 306
nodmonkey
  • 553
  • 1
  • 4
  • 5
  • 1
    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 08:11
  • Generally, I would use `Windows command line` in the title/body, along with the tags [tag:windows] and [tag:command-line] (note that [tag:cmd.exe] is a tag synonym of [tag:command-line]). The tag [tag:ms-dos] should be removed; and the tag [tag:dos] should not be used at all under any circumstances. – Bob Jun 24 '12 at 09: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 09:15
  • you can call it the cmd prompt – barlop Jan 08 '15 at 22:49
  • Why was this migrated from StackOverflow? This is about programming, so doesn't it belong there? – NewSites Aug 30 '19 at 12:52
  • @NewSites it's about general shell usage and not specifically about programming. See https://stackoverflow.com/help/on-topic https://superuser.com/help/on-topic – phuclv Mar 11 '22 at 12:35
  • You call it the command prompt. It's not appropriate to call it DOS because [DOS (actually command.com) and cmd.exe are very different](https://superuser.com/q/451432/241386) and has different capabilities and syntax – phuclv Mar 11 '22 at 12:36

4 Answers4

41

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
b w
  • 2,684
  • 4
  • 28
  • 33
  • 3
    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". – RomanSt Apr 30 '14 at 01:59
  • 5
    See the other answer - use a preceding `echo f | ` with xcopy to answer this prompt automatically. – mcw Aug 21 '14 at 20:18
  • 1
    Note that there should be a slash character, `\`, on the end of the destination path. Otherwise the command will prompt the user about whether the destination is a dir or a file if it doesn't exist. – Lii Sep 24 '18 at 09:17
22

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:

https://stackoverflow.com/questions/4283312/batch-file-asks-for-file-or-folder

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

msb
  • 1,437
  • 1
  • 15
  • 22
  • 1
    Which only works if there is no `file.txt.bak` in the directory, of course. Still, this might work in some scenarios. – RomanSt Apr 29 '14 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 '14 at 20:56
  • 1
    With `echo f | `, you no longer need the trailing `*`. – mcw Aug 21 '14 at 20:20
  • 2
    The second filename is redundant in case that it is the same. "xcopy file.txt c:\example\new\path\to\copy\of\file\" will work as long as the trailing slash in the directory is there (otherwise it asks if either a file or a directory weas meant). – FourtyTwo Jun 22 '16 at 13:51
  • Just use `xcopy file.txt "c:\example\new\path\to\copy\of\file\"` - it doesn't require interactoion. – Qwertiy Jul 23 '21 at 15:24
4

Whether using copy or xCopy, in order to avoid an error from copy or a prompt from xCopy, test for the existence of the needed folder and create it if necessary:

if not exist "NewPath" MkDir "NewPath"
copy "[path\]file.ext" "NewPath[\NewFileName.ext]"

or combine the commands with && on one command line:

if not exist "NewPath" MkDir "NewPath" && copy "[path\]file.ext" "NewPath[\NewFileName.ext]"

The same thing can be done with move instead of copy. I learned about this technique from a StackOverflow answer about how to do it with move.

NewSites
  • 610
  • 1
  • 6
  • 17
3

DOS, wow! Anyway you use the XCOPY command.

Dustin Laine
  • 223
  • 2
  • 5
  • 1
    4 years on: yup. DOS we all still use it even though PowerShell exists.. – Mr AH Oct 03 '14 at 00:04
  • Its 2021. We have PowerShell 7, brand new Terminal app, WSL2 — and we still use `xcopy` to do stuff. – simon Jun 03 '21 at 19:27