Windows 批处理命令移动目录中的所有文件夹,但有例外
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7461373/
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 batch command to move all folders in a directory with exceptions
提问by William Owen
I am trying to write a Windows Batch file that will allow me to move all directories within a given source directory into a target directory that exists within that source directory.
我正在尝试编写一个 Windows 批处理文件,该文件将允许我将给定源目录中的所有目录移动到该源目录中存在的目标目录中。
Obviously my move command with need to only apply to directories and also exclude the target directory from being processed.
显然,我的移动命令只需要应用于目录,并且从处理中排除目标目录。
Is this possible with a Windows batch command?
这可以通过 Windows 批处理命令实现吗?
回答by Alex K.
回答by Raymond Chen
FOR /d %%i IN (*) DO IF NOT "%%i"=="target" move "%%i" target
回答by WhoIsRich
Using robocopy included with Windows 7, I found the /XD option did not prevent the source folder from also being moved.
使用 Windows 7 附带的 robocopy,我发现 /XD 选项并没有阻止源文件夹也被移动。
Solution:
解决方案:
SET MoveDirSource=\Server\Folder
SET MoveDirDestination=Z:\Folder
FOR /D %%i IN ("%MoveDirSource%\*") DO ROBOCOPY /MOVE /E "%%i" "%MoveDirDestination%\%%~nxi"
This loops through the top level folders and runs robocopy for each.
这会遍历顶级文件夹并为每个文件夹运行 robocopy。
回答by Tony Woodhouse
NB: Robocopy mentioned above using the /move flag will copy the files and then delete them from the source folder rather than movingthe files. This may be critical if moving large numbers of files from one location to another on the same disk (because move is virtually instantaneous, while copying is a much slower operation)
注意:上面提到的 Robocopy 使用 /move 标志将复制文件,然后从源文件夹中删除它们,而不是移动文件。如果将大量文件从同一磁盘上的一个位置移动到另一个位置,这可能很关键(因为移动几乎是瞬时的,而复制操作要慢得多)
回答by eran
That won't work - you'll get an error telling you the target directory is inside the source directory or so, even if you explicitly exclude the target directory. What you can do is move the directories to a temporary location which is not under the source, and then move them into the target.
这是行不通的 - 即使您明确排除目标目录,您也会收到错误消息,告诉您目标目录在源目录内。您可以做的是将目录移动到不在源下的临时位置,然后将它们移动到目标中。
BTW, using the move
command won't let you specify folders to exclude. For that you can use xcopy
, but note that it will copythe folders, as opposed to movethem. If that matters, you can delete whatever you want afterwards, just make sure you don't delete the target dir, which is in the source dir...
顺便说一句,使用该move
命令不会让您指定要排除的文件夹。为此,您可以使用xcopy
,但请注意,它将复制文件夹,而不是移动它们。如果这很重要,您可以在之后删除任何您想要的内容,只需确保不要删除源目录中的目标目录...
回答by Oleg
This works for me:
这对我有用:
move c:\fromDir\*.* c:\toDir\
回答by River Rock
On windows batch:
在 Windows 批处理上:
FOR /d %%i IN (MySourceDirectory\*) DO move "%%i" MyTargetDirectory\%%~ni
The above command moves all directories found in MySourceDirectory (/d) to MyTargetDirectory using the original directory name (~ni) Robocopy's move first does a copy, then delete, so it is slower.
上面的命令把在MySourceDirectory(/d)中找到的所有目录移动到MyTargetDirectory,使用原来的目录名(~ni) Robocopy的move是先复制,再删除,所以比较慢。