How do you automate copying file with path too deep issues in Windows?

两盒软妹~` 提交于 2019-12-06 04:37:40

问题


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?

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.


回答1:


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:

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.

Once all files in a folder are copied, the recursion does jump back one level (subst /d), freeing up one drive letter.

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.


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.

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.




回答2:


Robocopy, part of the Windows Resource Kit, is designed to handle such cases.




回答3:


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.




回答4:


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.

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.




回答5:


As Sören suggested, try Robocopy:

robocopy empty_dir base_nested_dir /purge


来源:https://stackoverflow.com/questions/236533/how-do-you-automate-copying-file-with-path-too-deep-issues-in-windows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!