tags:

views:

47

answers:

2

Using VB 6

I want to select path only?

Selected Path - C:\Documents and Settings\Administrator\My Documents\1.txt

code:

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

From the above code i am getting.

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

I don't want to display last "\" also

Expected Output

C:\Documents and Settings\Administrator\My Documents

How to modify a code?

Need vb6 code Help.

+3  A: 
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
Wael Dalloul
+1  A: 

Wouldn't it be easier (and quicker as there's no For loop) to use InStrRev?

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

Note ... untested, but should work.

Chris J