How to copy a file to a directory in DOS, and create directories if necessary?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3 Looping
--
Chapters
00:00 How To Copy A File To A Directory In Dos, And Create Directories If Necessary?
00:52 Accepted Answer Score 43
01:31 Answer 2 Score 24
02:37 Answer 3 Score 4
03:13 Answer 4 Score 3
03:24 Thank you
--
Full question
https://superuser.com/questions/119263/h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#windows #cmdexe
#avk47
ACCEPTED ANSWER
Score 43
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
ANSWER 2
Score 24
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..
ANSWER 3
Score 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
.
ANSWER 4
Score 3
DOS, wow! Anyway you use the XCOPY command.