27

In a Windows batch file I am trying to move a file to a directory which may not currently exist. Because the directory is not there, when I do the move I see an error like:

The system cannot find the path specified

move c:\aaa\bbb\ccc\ddd\myfile.txt c:\aaa\111\222\333\444\mytext.txt

How can I easily create the path that I want to move to if it doesn't currently exist? For example here, directory 111 may not exist yet under aaa. I want the whole path structure to be created and then the file moved.
I had thought that it would just create the whole path for me as part of the move.

6 Answers 6

26

Try:

md c:\aaa\111\222\333\444 2> nul

before your Move command.

md makes directories recursive, so if there are no parent directories to 444, it will keep creating hierarchically. The "2> nul" ensures that if you have the directory already, your command wouldnt error out.

1
  • The answer by "Bali C" is better for the reason given in my comment on that answer.
    – NewSites
    Aug 30, 2019 at 12:22
25

If ROBOCOPY is an option, it will create the folder structure if it doesn't exist.

Try this:

ROBOCOPY c:\aaa\bbb\ccc\ddd c:\aaa\111\222\333\444 mytext.txt /MOV
5
  • Thanks but I have a question: you said: "If ROBOCOPY is an option". but is there a case where ROBOCOPY isn't an option? Is it unstable or something?
    – 123iamking
    Jul 29, 2019 at 15:36
  • 1
    Based on the date I answered this, I'm guessing I probably said that because Windows XP was still widely used and did not include ROBOCOPY.
    – aphoria
    Jul 29, 2019 at 17:29
  • 1
    I wonder if RoboCopy is an efficient way to do a move to a folder on the same drive. The documentation (help RoboCopy) says that /mov and /move work by "delete from source after copying". A move on the same drive should not require copying and deleting, but only changing the OS data showing the location of the file in the folder structure. So if all the moves are within the same drive, it's better to use the command move. Someone more knowledgeable please say whether that is correct.
    – NewSites
    Aug 30, 2019 at 12:34
  • @NewSites The original question did not ask for the most efficient move command, but just for way to create the folder structure for a move to a destination that may not exist. I don't know the how the internals of the robocopy MOVE or MOV switches work, but honestly it does not really matter for this question. The downvote is a bit much.
    – aphoria
    Aug 30, 2019 at 15:43
  • Sorry Aphoria, but when someone asks how to do something, I think they don't want just any way to do it, but the best way to do, or a selection of possible ways with a comparison of their pros and cons. There does not seem to be any advantage to using RoboCopy over Bali C's method, but there does seem to be a significant disadvantage by wasting a copy process that is not needed.
    – NewSites
    Aug 30, 2019 at 16:21
9
if not exist c:\aaa\111\222\333\444 md c:\aaa\111\222\333\444
Move c:\aaa\bbb\ccc\ddd\myfile.txt c:\aaa\111\222\333\444\mytext.txt
3
  • This is a better answer than the selected one because it does what is needed transparently. It avoids an error instead of ignoring one.
    – NewSites
    Aug 30, 2019 at 12:18
  • 1
    And if necessary, the two commands can be put on a single command line by using && to combine them: if not exist "NewPath" MkDir "NewPath" && move "[path\]file.ext" "NewPath[\NewFileName.ext]"
    – NewSites
    Aug 30, 2019 at 12:49
  • And further, the same technique can be used with copy instead of the klugey workaround echo f | xCopy .... See superuser.com/questions/119263/… .
    – NewSites
    Aug 30, 2019 at 13:18
4

Continuing on Aruns answer:

md c:\aaa\111\222\333\444\mytext.txt
rd c:\aaa\111\222\333\444\mytext.txt
move c:\aaa\bbb\ccc\ddd\myfile.txt c:\aaa\111\222\333\444\mytext.txt

This creates a folder called mytext.txt and its parents, and then deletes it, but not the parents.

More fun:

call :move_md "c:\aaa\bbb\ccc\ddd\myfile.txt" "c:\aaa\111\222\333\444\mytext.txt"
call :move_md "c:\aaa\bbb\ccc\ddd\myfile1.txt" "c:\aaa\111\222\333\444\mytext4.txt"
call :move_md "c:\aaa\bbb\ccc\ddd\myfile2.txt" "c:\aaa\111\222\333\444\mytext5.txt"
call :move_md "c:\aaa\bbb\ccc\ddd\myfile3.txt" "c:\aaa\111\222\333\444\mytext6.txt"
goto :eof

:move_md
md %2
rd %2
move %1 %2
goto :eof
0
2

Lets say you have the following directory structure.

C:\aaa\bbb\ccc\ddd

you want to create a directory called 111 under aaa, then 222 under 111, then 333 under 444 and so on

Window's cmd allows you to create a directory structure by providing multi level path

thus md c:\aaa\111\222\333\444 will create all the directory till 444.

You may want to create the directory first and then perform the move

0

Continuing on johvs answer: I really love the idea, but if you have a larger number of files this will not work due to performance limitations. It took ~8.5s per file, and I have about 3mio files to move, which makes a total computation time of roughly ten months. With this option out of the field I have found this 2-step-solution - first copy the folder structure, and only then copy the files:

  1. xcopy C:\source C:\target /t /e
  2. move C:\source\aaa\bbb\ccc\ddd\myfile.txt C:\target\111\222\333\444\mytext.txt

This obviously has the disadvantage that it will create unnecessary folders, but for my purpose it's not an issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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