Windows .BAT 将所有匹配掩码的目录从目录 A 移动到目录 B
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1501151/
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
Windows .BAT to move all directories matching mask from dir A to dir B
提问by shearichard
I want to write a .BAT file to move all sub-directories (whose name matches a mask) of C:\WINNT\Temp to H:\SOMEOTHERPLACE.
我想编写一个 .BAT 文件,将 C:\WINNT\Temp 的所有子目录(名称与掩码匹配)移动到 H:\SOMEOTHERPLACE。
So if my mask is ABC* then the directories :
因此,如果我的掩码是 ABC*,那么目录:
C:\WINNT\Temp\ABC1
C:\WINNT\Temp\ABC2
C:\WINNT\Temp\ABC3
should be moved to
应该移到
H:\SOMEOTHERPLACE
and everything else (including files, as opposed to directories, which match the mask) should not. I do want to move them and not copy.
和其他所有内容(包括文件,而不是与掩码匹配的目录)不应该。我确实想移动它们而不是复制。
Can anyone point me in the right direction ?
谁能指出我正确的方向?
回答by shearichard
OK I've figured this out. If you write a movedirs.bat file containing the single line
好的,我已经想通了。如果您编写了一个包含单行的movedirs.bat 文件
for /d %%X in (%1) do move %%X %2\%%~nX
And then run it (with argument 1 being the mask for the directories I want to move and argument 2 being the directory I wish to move the directories to) as
然后运行它(参数 1 是我要移动的目录的掩码,参数 2 是我希望将目录移动到的目录)作为
C:\>movedirs.bat C:\WINNT\Temp\ABC* H:\SOMEOTHERPLACE\
It produces the effect I want.
它产生了我想要的效果。
The /d argument on the 'for' ensures that only directories are processed. The '~n' modifier on the %%X variable means that the original sub-directory name (as opposed to the entire path) is used as the target within the second command line argument.
'for' 上的 /d 参数确保只处理目录。%%X 变量上的“~n”修饰符意味着原始子目录名称(而不是整个路径)用作第二个命令行参数中的目标。
Just for the sake of posterity in investigating this I did something similar with xcopy but then i would have had to get involved in deleting the source so for my purposes move works better but for the record here's the same idea wrapped around xcopy.
只是为了后代调查这个我做了一些与 xcopy 类似的事情,但后来我不得不参与删除源,所以为了我的目的,移动效果更好,但为了记录,这里围绕 xcopy 的相同想法。
for /d %%X in (%1) do xcopy %%X %2\%%~nX /E /I
To process directories with as well as without extensions, for example "C:\MyDir*.MyExt" above command will need a combined (filename+extension) modifier "~nx":
要处理带有和不带有扩展名的目录,例如“C:\MyDir*.MyExt”上面的命令将需要一个组合的(文件名+扩展名)修饰符“~nx”:
for /d %%W in (%1) do xcopy %%W %2\%%~nxW /E /F /R /Y /I
回答by shearichard
[Comments are useless for structured answers so I'll repeat the comment here - with a couple of edits !]
[评论对于结构化答案毫无用处,所以我会在这里重复评论 - 进行一些编辑!]
Thanks for your answer but I've found that you can't use xcopy with a wildcard on the source. Or rather you can use wildcards but then you get only the directories being created without any content. So if you do this ...
感谢您的回答,但我发现您不能在源代码上使用带有通配符的 xcopy。或者更确切地说,您可以使用通配符,但是您只会获得正在创建的目录,而没有任何内容。所以如果你这样做...
H:\SOMEOTHERPLACE>xcopy C:\WINNT\Temp\ABC1 /E
... you'll get the the ABC1 directory copied to your current directory as you might reasonably expect but if you do this ...
...您将按照合理预期将 ABC1 目录复制到当前目录,但如果您这样做...
H:\SOMEOTHERPLACE>xcopy C:\WINNT\Temp\ABC* /E
... you'll get each directory name present in C:\WINNT\Temp appearing in your current directory but those directories will be empty ! Please tell me I'm wrong but that's what I find !
...您将在当前目录中出现 C:\WINNT\Temp 中的每个目录名称,但这些目录将为空!请告诉我我错了,但这就是我发现的!