tags:

views:

1287

answers:

5

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.

+2  A: 

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.

Tomalak
+7  A: 

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

Sören Kuklau
Just as an addition, Robocopy is part of Windows Vista (and likely of Sever 2008 and any future Windows version).
OregonGhost
I may not be reading the documentation correctly, but I don't see how you can use robocopy to selectively only copy one file and it's directory structure. I tested it on a directory with a deep path but it appears only directories can be specified and not specific files.
llamaoo7
+3  A: 

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.

OregonGhost
+1  A: 

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.

John Dyer
A: 

As Sören suggested, try Robocopy:

robocopy empty_dir base_nested_dir /purge
Gonsalu