在访问97中查找完整路径的目录部分(减去文件名)

ape*_*arr 24 excel ms-access vba excel-vba ms-access-97

由于各种原因,我陷入了Access 97并且只需要获取完整路径名的路径部分.

例如,名称

c:\whatever dir\another dir\stuff.mdb
Run Code Online (Sandbox Code Playgroud)

应该成为

c:\whatever dir\another dir\
Run Code Online (Sandbox Code Playgroud)

该网站提供了一些有关如何操作的建议:http: //www.ammara.com/access_image_faq/parse_path_filename.html

但它们看起来相当可怕.必须有更好的方法,对吧?

Mak*_*kah 41

你可以做一些简单的事情: Left(path, InStrRev(path, "\"))

例:

Function GetDirectory(path)
   GetDirectory = Left(path, InStrRev(path, "\"))
End Function
Run Code Online (Sandbox Code Playgroud)

  • 冠军!!迄今为止最好的解决方案! (2认同)

Joh*_* Mo 19

我总是用FileSystemObject这种东西.这是我使用的一个小包装函数.一定要参考Microsoft Scripting Runtime.

Function StripFilename(sPathFile As String) As String

'given a full path and file, strip the filename off the end and return the path

Dim filesystem As New FileSystemObject

StripFilename = filesystem.GetParentFolderName(sPathFile) & "\"

Exit Function

End Function
Run Code Online (Sandbox Code Playgroud)

  • 从什么时候开始引用一个坏主意?访问本身需要参考工作.0_o (10认同)
  • 不好主意,因为它需要参考工作.如果你坚持,你应该使用后期绑定. (2认同)

小智 10

这似乎有效.以上不适用于Excel 2010.

Function StripFilename(sPathFile As String) As String
'given a full path and file, strip the filename off the end and return the path
Dim filesystem As Object

Set filesystem = CreateObject("Scripting.FilesystemObject")

StripFilename = filesystem.GetParentFolderName(sPathFile) & "\"

Exit Function

End Function
Run Code Online (Sandbox Code Playgroud)