给出以下代码段:
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,则可以正常工作,并显示找到和未找到文件的预期行为。
是否可以使用长(> 260)路径名来使DIR工作?
笔记
最佳答案
这是一些无论深度如何都应该起作用的代码...
基本上,它指定相对路径-因此,您永远不会用长字符串调用dir
Function deepFileExists(longFileName As String)
' slowly make your way to the deepest folder...
' assuming "\" is used as separator
' you could add some code to replace "/" with "\"...
Dim pathFragment As String, currentDir As String
Dim slash As Integer, lastSlash As Integer
slash = InStr(1, longFileName, "\")
lastSlash = 0
pathFragment = Mid(longFileName, 1, slash - 1)
currentDir = CurDir ' save the current directory
ChDrive pathFragment ' making sure we have the right drive
ChDir pathFragment & "\" ' be at the root of this drive's directory
lastSlash = slash
slash = InStr(slash + 1, longFileName, "\")
While (slash > 0)
pathFragment = ".\" & Mid(longFileName, lastSlash + 1, slash - lastSlash)
ChDir pathFragment
'MsgBox "changing directory to " & pathFragment
lastSlash = slash
slash = InStr(slash + 1, longFileName, "\")
Wend
' now we can look for the file:
Dim a
a = Dir(Mid(longFileName, lastSlash + 1))
If Len(a) > 0 Then
deepFileExists = True
Else
deepFileExists = False
End If
End Function
关于vba - VBA中的DIR(path)是否可以处理长度大于260的字符串?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14720710/