Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
765 views
in Technique[技术] by (71.8m points)

excel - Is there a way for DIR(path) in VBA to handle strings longer than 260?

Given the following snippet:

Dim s As String: s = "S:vicla[..insert more here..]data.xml"
Debug.Print Len(s)
Debug.Print Dir(s)

If Len(s) >= 260 I receive an error stating the following:

Run-time error '53':

File not found

If the string is less than 260 it works fine and displays expected behavior for both found and non-found files.

Is there to get DIR working with long (>260) path names?

Notes

  • File restructure is not an option

  • I am running this in Excel 2007

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Shortly put (to answer the answer as titled): No. VBA's Dir function simply does not work with paths beyond 260 characters.

Long version: http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx#maximum_path_length (then Ctrl+F and search for "260")

Maximum Path Length Limitation

In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters. A local path is structured in the following order: drive letter, colon, backslash, name components separated by backslashes, and a terminating null character. For example, the maximum path on drive D is "D:some 256-character path string" where "" represents the invisible terminating null character for the current system codepage. (The characters < > are used here for visual clarity and cannot be part of a valid path string.) Note File I/O functions in the Windows API convert "/" to "" as part of converting the name to an NT-style name, except when using the "?" prefix as detailed in the following sections. The Windows API has many functions that also have Unicode versions to permit an extended-length path for a maximum total path length of 32,767 characters. This type of path is composed of components separated by backslashes, each up to the value returned in the lpMaximumComponentLength parameter of the GetVolumeInformation function (this value is commonly 255 characters). To specify an extended-length path, use the "?" prefix. For example, "?D:very long path". Note The maximum path of 32,767 characters is approximate, because the "?" prefix may be expanded to a longer string by the system at run time, and this expansion applies to the total length.

I think the section about Win32 File NameSpaces is worth giving a try:

For file I/O, the "?" prefix to a path string tells the Windows APIs to disable all string parsing and to send the string that follows it straight to the file system. For example, if the file system supports large paths and file names, you can exceed the MAX_PATH limits that are otherwise enforced by the Windows APIs. For more information about the normal maximum path limitation, see the previous section Maximum Path Length Limitation.

There must be a Win32 API function you can DECLARE and use, but that's not using the DIR function. Sorry, don't have a long path name at hand to test anything...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...