如何在没有最后的情况下获得路径?

时间:2009-09-10 09:59:33

标签: vb6

使用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代码帮助。

2 个答案:

答案 0 :(得分: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 :(得分: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

注意......未经测试,但应该有效。