gpt4 book ai didi

vba - VBA 中的 DIR(path) 有没有办法处理长度超过 260 的字符串?

转载 作者:行者123 更新时间:2023-12-03 04:57:40 26 4
gpt4 key购买 nike

给出以下代码片段:

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

如果 Len(s) >= 260 我收到一条错误,内容如下:

Run-time error '53':

File not found

如果字符串小于 260,则它可以正常工作并显示找到的文件和未找到的文件的预期行为。

是否可以让 DIR 使用长(>260)路径名?

注释

  • 文件重组不是一个选项

  • 我在 Excel 2007 中运行此程序

最佳答案

简而言之(回答标题中的答案):不。VBA 的 Dir 函数根本无法处理超过 260 个字符的路径。

长版:http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx#maximum_path_length (然后按 Ctrl+F 并搜索“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.

我认为关于Win32 文件命名空间的部分值得一试:

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.

必须有一个可以DECLARE并使用的Win32 API函数,但这不是使用DIR函数。抱歉,手头没有长路径名来测试任何内容...

关于vba - VBA 中的 DIR(path) 有没有办法处理长度超过 260 的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14720710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com