Windows 批量移动到可能不存在的目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7264736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:44:50  来源:igfitidea点击:

Windows Batch move to directory that may not exist

windowsbatch-file

提问by George Hernando

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:

在 Windows 批处理文件中,我试图将文件移动到当前可能不存在的目录中。因为目录不存在,所以当我移动时,我看到如下错误:

The system cannot find the path specified

该系统找不到指定的路径

move c:\aaa\bbb\ccc\ddd\myfile.txt c:\aaa1234\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.

如果当前不存在,如何轻松创建要移动到的路径?例如这里,目录 111 在 aaa 下可能还不存在。我希望创建整个路径结构,然后移动文件。
我原以为它只会为我创建整个路径作为移动的一部分。

回答by Arun

Try:

尝试:

md c:\aaa1234 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.

md 使目录递归,因此如果 444 没有父目录,它将继续分层创建。“2> nul”确保如果您已经拥有该目录,则您的命令不会出错。

回答by aphoria

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

如果ROBOCOPY是一个选项,如果它不存在,它将创建文件夹结构。

Try this:

尝试这个:

ROBOCOPY c:\aaa\bbb\ccc\ddd c:\aaa1234 mytext.txt /MOV

回答by Bali C

if not exist c:\aaa1234 md c:\aaa1234
Move c:\aaa\bbb\ccc\ddd\myfile.txt c:\aaa1234\mytext.txt

回答by johv

Continuing on Aruns answer:

继续阿伦斯的回答:

md c:\aaa1234\mytext.txt
rd c:\aaa1234\mytext.txt
move c:\aaa\bbb\ccc\ddd\myfile.txt c:\aaa1234\mytext.txt

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

这将创建一个名为mytext.txt及其父文件夹的文件夹,然后删除它,但不删除父文件夹。

More fun:

更多乐趣:

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

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

回答by jsshah

Lets say you have the following directory structure.

假设您有以下目录结构。

C:\aaa\bbb\ccc\ddd

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

您想在 aaa 下创建一个名为 111 的目录,然后是 111 下的 222,然后是 444 下的 333 等等

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

Window 的 cmd 允许您通过提供多级路径来创建目录结构

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

因此 md c:\aaa\111\222\333\444将创建所有目录,直到 444。

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

您可能希望先创建目录,然后执行移动