如何在 Windows 中自动复制路径过深问题的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/236533/
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
How do you automate copying file with path too deep issues in Windows?
提问by llamaoo7
I want to be able to selectively copy a list of files and preserve their directory structure. The problem is that there are quite a few files that their path exceeds 256 character. How is this problem usually handled?
我希望能够有选择地复制文件列表并保留它们的目录结构。问题是有很多文件的路径超过 256 个字符。这个问题一般是怎么处理的?
Edit: I should make it clear that I only want to selectively copy files, not folders. I don't think robocopy can be efficiently used to copy an individual file and it's folder structure effectively.
编辑:我应该明确表示我只想有选择地复制文件,而不是文件夹。我认为 robocopy 不能有效地用于复制单个文件及其文件夹结构。
采纳答案by Tomalak
I wrote a VBscript that checks path length and calls subst
, as soon as a certain threshold is reached. These calls are stacked on each other so that in the middle of a recursion, this layout exists:
我编写了一个 VBscript,它subst
会在达到某个阈值后立即检查路径长度和调用。这些调用相互堆叠,因此在递归的中间,存在以下布局:
C:\a\very\long\path
subst K: "C:\a\very\long\path"
K:\another\very\long\path
subst L: "K:\another\very\long\path"
L:\yet\another\very\long\path
subst M: "L:\yet\another\very\long\path"
xcopy M:\*.* "D:\target"
This way with each level of subst, a shorter path is generated. It also means, that you must copy your folders sequentially, to be able check for long paths before you issue the copy command.
这样,对于每一级 subst,都会生成一条更短的路径。这也意味着,您必须按顺序复制文件夹,以便在发出复制命令之前检查长路径。
Once all files in a folder are copied, the recursion does jump back one level (subst /d
), freeing up one drive letter.
复制文件夹中的所有文件后,递归会跳回一级 ( subst /d
),释放一个驱动器号。
Using 4-5 drive letters, that subst each other when the path gets to deep I had been able to copy paths that had lengths waaaaay over the MAX_PATH limit.
使用 4-5 个驱动器号,当路径变深时,它们相互替代,我已经能够复制长度超过 MAX_PATH 限制的路径。
EDIT
编辑
This describes the general procedure of doing it with subst. How you do it depends on your needs, I always used that little subst trick in a minimal, "solves this single problem" way.
这描述了使用 subst 执行此操作的一般过程。你怎么做取决于你的需要,我总是以最小的方式使用这个小技巧,“解决这个单一的问题”。
For example, copying to an equally deep target path means you need another stack of subst'ed drive letters.
例如,复制到同样深的目标路径意味着您需要另一堆替换的驱动器号。
Unpacking all .zip files within a single, deeply nested directory structure may require only on stack, but you need to shorten the threshold a bit to account for folders in the .zip, etc.
在单个深度嵌套的目录结构中解压所有 .zip 文件可能只需要堆栈,但您需要稍微缩短阈值以考虑 .zip 中的文件夹等。
回答by S?ren Kuklau
Robocopy, part of the Windows Resource Kit, is designed to handle such cases.
Robocopy 是Windows Resource Kit 的一部分,旨在处理此类情况。
回答by OregonGhost
Do you want to implement the copy procedure for yourself? If so, did you try UNC paths? I never had to work with them, but put simple you use a prefix and the path can be much longer than MAX_PATH, as described in this MSDN article.
您想自己实现复制程序吗?如果是这样,您是否尝试过 UNC 路径?我从来没有用过它们,但简单地说,你使用前缀,路径可以比 MAX_PATH 长得多,如这篇 MSDN 文章中所述。
回答by John Dyer
A hack from the old days is to assign a drive letter to part of the directory.
过去的一个技巧是为目录的一部分分配一个驱动器号。
One option is the SUBST command. That will allow you to substitute a drive letter for a drive letter and path combination. I have seen this done in install packages to get a shorter version of the directory.
一种选择是 SUBST 命令。这将允许您用驱动器号替换驱动器号和路径组合。我已经在安装包中看到过这样做以获得更短版本的目录。
Alternativly you can use a shared folder and map it to a drive letter. Or you can use the an administrative share.
或者,您可以使用共享文件夹并将其映射到驱动器号。或者您可以使用管理共享。
But really if you have code that you can run, then handling it there is much better that these hacks.
但实际上,如果您有可以运行的代码,那么处理它比这些黑客要好得多。
回答by gonsalu
As S?ren suggested, try Robocopy:
正如 S?ren 建议的那样,试试 Robocopy:
robocopy empty_dir base_nested_dir /purge