2009-09-10 93 views
0

使用VB 6如何獲得只有最後路徑?

我想只選擇路徑嗎?

路徑選擇 - C:\Documents and Settings\Administrator\My Documents\1.txt

代碼:

Public Function Getpath01(sFile As String) As String 
    Dim iPos As Long 
    For iPos = Len(sFile) To 1 Step -1 
    If Mid$(sFile, iPos, 1) = "\" Then 
     Getpath01 = Left$(sFile, iPos) 
     Exit Function 
    End If 
    Next 
    Getpath01 = sFile 
End Function 

從上面的代碼我收到。

C:\Documents and Settings\Administrator\My Documents\ 

我不想顯示最後一個 「\」 也

預期輸出

C:\Documents and Settings\Administrator\My Documents 

如何修改密碼?

需要vb6代碼幫助。

回答

3
Public Function Getpath01(sFile As String) As String 
    Dim iPos As Long 
    For iPos = Len(sFile) To 1 Step -1 
    If Mid$(sFile, iPos, 1) = "\" Then 
     Getpath01 = Left$(sFile, iPos-1) 
     Exit Function 
    End If 
    Next 
    Getpath01 = sFile 
End Function 
1

使用InStrRev會不會更容易(而且更快,因爲沒有For循環)?

Public Function GetPath01 (sFile as string) as string 
    Dim iPos As Long 

    iPos = InStrRev(sFile, "\") 
    If iPos > 0 Then 
     GetPath01 = Left$(sFile, iPos - 1) 
    Else 
     GetPath01 = sFile 
    End If 
End Function 

注意...未經測試,但應該工作。