VB 从全路径中获取文件名

VB 从全路径中获取文件名
2024-12-03 16:15:34
推荐回答(5个)
回答1:

Public Function GetFileName(ByVal FullPathName As String, Optional ByVal NoExtName As Boolean = False) As String
'从指定全路径中找到文件名
'FullPathName指定全路径
'返回值:包含的文件名
Dim I As Long, J As Long
Dim filename As String, FileNameNoExt As String

FullPathName = Trim(FullPathName)
I = InStrRev(FullPathName, "\")
J = Len(FullPathName)
If I = 0 Then Exit Function

filename = Mid(FullPathName, I + 1, J - I)
I = InStrRev(filename, ".")
J = Len(filename)
If I = 0 Then Exit Function

FileNameNoExt = Mid(filename, 1, I - 1)
If NoExtName = True Then
GetFileName = FileNameNoExt
Else
GetFileName = filename
End If

End Function

回答2:

Dim Wenjianming As String '文件名
Dim lujingB As String '半个路径,既没有文件名的目录
Dim N1 As Integer
N1 = InStrRev(Lujing1, "\", , vbTextCompare)
Wenjianming = Right$(Lujing1, Len(Lujing1) - N1)
lujingB = Left$(Lujing1, N1)
MsgBox Wenjianming
MsgBox lujingB

回答3:

guduguoker的答案正确

回答4:

dim Path as string,FileName as string ,S() as string
Path=路径
s=split(path,"\")
FileName=s(ubound(s))

回答5:

判断最后一个“/”的位置,然后根据此数据取后面的字符即可。