传入带全路径的文件字符串,返回文件名和路径

Public Function GetFileName(sFile As String) As String
    '传入带全路径的字符串,返回文件名
    Dim i As Long, s As String, s1 As String
    s1 = StrReverse(sFile)
    For i = 1 To Len(sFile)
        s = Mid(s1, i, 1)
        If s = "/" Then
            GetFileName = StrReverse(Left(s1, i - 1))
            Exit For
        End If
    Next
End Function

 

Public Function GetFilePath(sFile As String) as String

    '传入带全路径的字符串,返回文件路径

    Dim i as long, ss() As String

    ss = Split(sFile , "/") i = Len(ss(UBound(ss)))

    GetFilePath= Left(sFile , Len(sFile ) - i)

End Function

 

 

方法二:

用InStrRev函数
描述
返回一个字符串在另一个字符串中出现的位置,从字符串的末尾算起。

 

Public Function GetFilePath(sFile As String) as String 

    '传入带全路径的字符串,返回文件路径Dim i As Long
    i
= InStrRev(sFile, "/")
    GetFilePath =
Left(sFile, i - 1)
End Function

   

你可能感兴趣的:(String,function)