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


The Definition
Function GetFile
Returns the file part from a full path file name.
Declaration
Function GetFile(sFile As String) As String
Arguments
sFilefull path file name
sFile Values  => Return Values
"c:\dir\file.txt""file.txt"
"c:\dir\"""
"c:\dir""dir"
"file.txt""file.txt"
""""
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""file.txt"
"c:\dir\""dir" (!)
"c:\dir""dir"
"file.txt""file.txt"
""""


The Charts
Calls
 sRet = GetFile(sFile)
Call 1 sFile = "C:\Programs\Microsoft Office\Office\Winword.exe"
 VB5
CodeAuthorDopingNotes
GetFile01 Nobody  
GetFile02 Donald  
GetFile03Peter VB6
GetFile04 PeterFSO 
GetFile05 ChrisAPI 
Call 1
414.4921.451Ás
32.523.732Ás
   
22.173.218Ás
11.001.480Ás
 VB6
CodeAuthorDopingNotes
GetFile01 Nobody  
GetFile02 Donald  
GetFile03 Peter VB6
GetFile04 PeterFSO 
GetFile05 ChrisAPI 
Call 1
513.8820.808Ás
42.403.591Ás
32.253.372Ás
22.243.351Ás
11.001.499Ás
Conclusions
Comparing 01/02: minimizing your calls to VB string functions gets you on the speedway.
The FileSystemObject does it, but note that it does it slightly differently from our specification.
Mail your code! How to read all those numbers


The Code
GetFile01
submitted 10-Oct-2000 by Donald Lessau  
Doping: none
Public Function GetFile01(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
      GetFile01 = Mid$(sFile, iPos + 1)
      Exit Function
    End If
  Next
  ' no backslash found
  GetFile01 = sFile
End Function
Author's comments:
Donald's comments:

top | charts


GetFile02
submitted 10-Oct-2000 by Donald Lessau  
Doping: none
Public Function GetFile02(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
    GetFile02 = Mid$(sFile, iPosPrev + 1)
  Else
    GetFile02 = sFile
  End If
  
End Function
Author's comments:
Donald's comments:

top | charts


GetFile03
submitted 20-Oct-2000 by Peter Weighill  
Doping: none
Public Function GetFile03(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
    GetFile03 = Mid$(sFile, iPos + 1)
  Else
    GetFile03 = sFile
  End If
End Function
Author's comments:
Donald's comments:

top | charts


GetFile04
submitted 22-Oct-2000 by Peter Weighill  
Doping: FSO
Public Function GetFile04(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
    GetFile04 = fso.GetFileName(sFile)
End Function
Author's comments: In real life use, you would probably make fso a global object.
Donald's comments:

top | charts


GetFile05
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