VBspeed / String / GetPath
VBspeed © 2000-10, updated: 22-Oct-2000
GetPath


The Definition
Function GetPath
Returns the path part from a full path file name.
Declaration
Function GetPath(sFile As String) As String
Arguments
sFilefull path file name
sFile Values  => Return Values
"c:\dir\file.txt""c:\dir\"
"c:\dir\""c:\dir\"
"c:\dir""c:\"
"c:""c:"
""""
Use this function (VB5/6-compatible) to verify the correctness of your emulation code.
Using the FileSystemObject:
sFile Values  => Return Values
"c:\dir\file.txt""c:\dir" (!)
"c:\dir\""c:\" (!)
"c:\dir""c:\"
"c:""" (!)
""""


The Charts
Calls
 sRet = GetPath(sFile)
Call 1 sFile = "C:\Programs\Microsoft Office\Office\Winword.exe"
 VB5
CodeAuthorDopingNotes
GetPath01 Nobody  
GetPath02 Donald  
GetPath03Peter VB6
GetPath04 PeterFSO 
GetPath05 ChrisAPI 
Call 1
414.2821.440Ás
22.463.695Ás
   
32.774.165Ás
11.001.502Ás
 VB6
CodeAuthorDopingNotes
GetPath01 Nobody  
GetPath02 Donald  
GetPath03 Peter VB6
GetPath04 PeterFSO 
GetPath05 ChrisAPI 
Call 1
513.1323.073Ás
32.083.661Ás
21.953.418Ás
42.624.594Ás
11.001.757Ás
Conclusions
Comparing 01/02: minimizing your calls to VB string functions gets you on the speedway.
Note that the FileSystemObject (GetParentFolderName) does it quite differently from our specification.
Mail your code! How to read all those numbers


The Code
GetPath01
submitted 10-Oct-2000 by Donald Lessau  
Doping: none
Public Function GetPath01(sFile As String) As String
' by anonymous, common bad solution
  Dim iPos As Long
  ' search last backslash
  For iPos = Len(sFile) To 1 Step -1
    If Mid$(sFile, iPos, 1) = "\" Then
      GetPath01 = Left$(sFile, iPos)
      Exit Function
    End If
  Next
  ' no backslash found
  GetPath01 = sFile
End Function
Author's comments:
Donald's comments:

top | charts


GetPath02
submitted 10-Oct-2000 by Donald Lessau  
Doping: none
Public Function GetPath02(sFile As String) As String
' by Donald, donald@xbeat.net, 20001010
  Dim iPos As Long
  Dim iPosPrev As Long
  
  ' search last backslash
  Do
    iPosPrev = iPos
    iPos = InStr(iPos + 1, sFile, "\", vbBinaryCompare)
  Loop While iPos > 0
  
  If iPosPrev > 0 Then
    GetPath02 = Left$(sFile, iPosPrev)
  Else
    GetPath02 = sFile
  End If
  
End Function
Author's comments:
Donald's comments:

top | charts


GetPath03
submitted 20-Oct-2000 by Peter Weighill  
Doping: none
Public Function GetPath03(sFile As String) As String
' by Peter Weighill, pweighill@btinternet.com, 20001020
' Only for VB6
  Dim iPos As Long
  
  ' search last backslash
  iPos = InStrRev(sFile, "\", -1, vbBinaryCompare)
  
  If iPos > 0 Then
    GetPath03 = Left$(sFile, iPos)
  Else
    GetPath03 = sFile
  End If
End Function
Author's comments:
Donald's comments:

top | charts


GetPath04
submitted 22-Oct-2000 by Peter Weighill  
Doping: FSO
Public Function GetPath04(sFile As String) As String
' by Peter Weighill, pweighill@btinternet.com, 20001022
' Requires Reference to Microsoft Scripting Runtime
    Static fso As Scripting.FileSystemObject

    If fso Is Nothing Then
        Set fso = New Scripting.FileSystemObject
    End If
    GetPath04 = fso.GetParentFolderName(sFile)
End Function
Author's comments: In real life use, you would probably make fso a global object.
Donald's comments:

top | charts


GetPath05
submitted 04-Dec-2001 by Chris Lucas  
Doping: API.
GetExtension06, GetFile05, GetPath05 all wrapped into one handy class.
Cinemascope here, preview here:

Author's comments :
Donald's comments :

top | charts




VBspeed © 2000-10 by Donald Lessau