“cp——父母”在批处理文件/VBScript 中

发布于 2024-08-05 21:41:30 字数 293 浏览 3 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坏尐絯℡ 2024-08-12 21:41:30

这应该有效:

for /r %%a in (*.cmd) do xcopy %%a C:\DESTINATION%%~pa

请注意,DESTINATION 绝不应该是您要从中复制的目录的子目录,否则 for /r 会进入递归循环复制它已经复制的文件,创建越来越长的目录路径(不要问我怎么知道)。

您可以使用 xcopy /c 使其更加健壮(即使发生错误也可以继续复制)。您可能还想查看 xcopy /? 看看是否还有其他有价值的内容(/q/r/o 等)。

This should work:

for /r %%a in (*.cmd) do xcopy %%a C:\DESTINATION%%~pa

Note that DESTINATION should never be a subdirectory of the directory you are trying to copy from, otherwise for /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 at xcopy /? to see if there is anything else of value there (/q, /r, /o, etc).

杀お生予夺 2024-08-12 21:41:30

编辑:
糟糕的是,我错过了 cp 命令中的 --parents 标志...如下所示的 xcopy 将仅重新创建向下路径,而不是当前目录“上方”的目录。

我[现在]相信xcopy [不能][单独完成。但格兰特·瓦格纳的“for /r”技巧却能创造奇迹]。

xcopy [some_path]*.ext [some_otherpath]\final_directory /E  

 (or /S, above, if you don't want empty directories)
 the elements within brackets, some_path and some_otherpath are optional

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].

xcopy [some_path]*.ext [some_otherpath]\final_directory /E  

 (or /S, above, if you don't want empty directories)
 the elements within brackets, some_path and some_otherpath are optional
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文