VB6:从文件路径获取文件夹名称的简单方法
如果我有文件的完整路径:VB6:从文件路径获取文件夹名称的简单方法,vb6,filepath,Vb6,Filepath,如果我有文件的完整路径: eg. c:\files\file.txt 获取此文件文件夹的最简单方法是什么:例如c:\files\?您可以使用InStrRev搜索\,使用Left$提取路径位: filename = "c:\files\file.txt" posn = InStrRev(filename, "\") If posn > 0 Then pathstr = Left$(filename, posn) Else pathstr = "" End If 为了便于使
eg. c:\files\file.txt
获取此文件文件夹的最简单方法是什么:
例如c:\files\
?您可以使用InStrRev
搜索\
,使用Left$
提取路径位:
filename = "c:\files\file.txt"
posn = InStrRev(filename, "\")
If posn > 0 Then
pathstr = Left$(filename, posn)
Else
pathstr = ""
End If
为了便于使用,我将使用它制作一个函数:
Function pathOfFile(fileName As String) As String
Dim posn As Integer
posn = InStrRev(fileName, "\")
If posn > 0 Then
pathOfFile = Left$(filename, posn)
Else
pathOfFile = ""
End If
End Function
使用
FileSystemObject.GetParentFolderName(strFullFilePath)
Dim strFullFilePath As String
strFullFilePath = "c:\files\file.txt"
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
MsgBox fso.GetParentFolderName(strFullFilePath)
请注意,这会返回
c:\file
,而不是c:\file\
注释副本的可能副本的精确副本……我意识到这可能是一个无辜的意外,但您已经发布了一个,然后很快回答了您自己的问题:并且您的答案中的解决方案与类似。我不是在指责你有任何不正当的行为,我相信这是偶然的(重复的问题有一个糟糕的标题),但我们应该结束这个问题。
' GetFilenameWithoutExtension: Return filename without extension from complete path
Public Function GetFilenameWithoutExtension(path As String) As String
Dim pos As Integer
Dim filename As String
pos = InStrRev(path, "\")
If pos > 0 Then
filename = Mid$(path, pos + 1, Len(path))
GetFilenameWithoutExtension = Left(filename, Len(filename) - Len(Mid$(filename, InStrRev(filename, "."), Len(filename))))
Else
GetFilenameWithoutExtension = ""
End If
End Function
' GetFilenameWithExtension: Return filename with extension from complete path
Public Function GetFilenameWithExtension(path As String) As String
Dim pos As Integer
pos = InStrRev(path, "\")
If pos > 0 Then
GetFilenameWithExtension = Mid$(path, pos + 1, Len(path))
Else
GetFilenameWithExtension = ""
End If
End Function
' GetDirectoryFromPathFilename: Return directory path contain filename
Public Function GetDirectoryFromPathFilename(path As String) As String
Dim pos As Integer
pos = InStrRev(path, "\")
If pos > 0 Then
GetDirectoryFromPathFilename = Left$(path, pos)
Else
GetDirectoryFromPathFilename = ""
End If
End Function