“cp——父母”在批处理文件/VBScript 中
在 Windows 环境中只有批处理文件和 VBScript 可供使用时,您会如何编写此代码?
find -name '*.ext' -exec cp --parents {} destination/ \;
或者这样说:
- 在当前文件夹中递归查找某些文件名模式(在上面的示例中以名为
ext
的扩展名结尾), - 将这些文件中的每一个复制到目标文件夹(无论在哪里)保留当前文件夹中的目录树结构(或创建任何丢失的中间目录)。
How would you write this having only batch files and VBScript at your disposal in a Windows environment?
find -name '*.ext' -exec cp --parents {} destination/ \;
Or put it this way:
- Look recursively in the current folder for some filename pattern (ending with an extension called
ext
in the example above), - Copy each of these files to a destination folder (wherever that is) preserving directory tree structure (or creating any missing intermediate directories) as in current folder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该有效:
请注意,
DESTINATION
绝不应该是您要从中复制的目录的子目录,否则for /r
会进入递归循环复制它已经复制的文件,创建越来越长的目录路径(不要问我怎么知道)。您可以使用 xcopy /c 使其更加健壮(即使发生错误也可以继续复制)。您可能还想查看
xcopy /?
看看是否还有其他有价值的内容(/q
、/r
、/o
等)。This should work:
Note that
DESTINATION
should never be a subdirectory of the directory you are trying to copy from, otherwisefor /r
goes into a recursive loop copying files it has already copied creating longer and longer directory paths (don't ask me how I know).You could make it slightly more robust using
xcopy /c
(to continue copying even if errors occur). You may also want to look atxcopy /?
to see if there is anything else of value there (/q
,/r
,/o
, etc).编辑:
糟糕的是,我错过了 cp 命令中的 --parents 标志...如下所示的 xcopy 将仅重新创建向下路径,而不是当前目录“上方”的目录。
我[现在]相信xcopy [不能][单独完成。但格兰特·瓦格纳的“for /r”技巧却能创造奇迹]。
Edit:
My bad, I missed the --parents flag in cp command... xcopy as shown below will on only recreated the downward path, not that of the directories "above" current directory.
I [now] believe xcopy would [not] do the trick [alone. But works wonders with Grant Wagner's "for /r" trick].