考考你: 如何用最简洁的代码从字符串中解析出"目录"、"文件名"和"扩展名"??
http://www.csdn.net/Expert/TopicView1.asp?id=219194

解决方案 »

  1.   

    '****函数1:从全路径取文件名(带扩展名)
    Function getShortFileNameWithExt(fullFilename)
        Dim filename
        On Error Resume Next
            filename = Dir(fullFilename)
        
            getShortFileNameWithExt = filename
            
    End Function
    '****函数1:从全路径取扩展名
    Function getFileExtName(fullFilename)
        
        'InstrRev(string1, string2[, start[, compare]])
        Dim filename
        
            filename = Dir(fullFilename)
        
            filename = Mid(filename, InStrRev(filename, ".") + 1, Len(filename) - InStrRev(filename, "."))
        
        
        getFileExtName = filenameEnd Function
    '****函数1:从全路径取文件名(不带扩展名)
    Function getShortFileNameNoExt(fullFilename)
        Dim filename
        
            filename = Dir(fullFilename)
        
            filename = Mid(filename, 1, InStr(1, filename, ".") - 1)
        
        getShortFileNameNoExt = filename
    End Function
      

  2.   

    Private Sub Command3_Click()    MsgBox test("c:\asdasa\asdasd\你好asd\你好.asd.dat")
    End Sub
    Function test(vvv As String) As String
        Dim tmp As String
        Dim ll As Integer
        
        test = ""
        tmp = vvv
        Do Until InStr(1, tmp, "\") = 0
            ll = InStr(1, tmp, "\")
            tmp = Right(tmp, Len(tmp) - ll)
        Loop
        
        Do Until InStr(1, tmp, ".") = 0
            ll = InStr(1, tmp, ".")
            test = test & Left(tmp, ll)
            tmp = Right(tmp, Len(tmp) - ll)
        Loop
        If test = "" Then
            test = tmp
        Else
            test = Left(test, Len(test) - 1)
        End If
        
    End Function
      

  3.   

    '从带路径及后缀名的字符串中分离出文件名
    Public Function GetProgramName(ByVal PathFile As String) As String
    Dim i As Integer, p As Stringi = InStrRev(PathFile, "\")
    p = Right(PathFile, Len(PathFile) - i)i = InStrRev(p, ".")
    GetProgramName = Left(p, i - 1)End Function