VBspeed / String / GetExtension
VBspeed © 2000-10, updated: 09-Dec-2000
GetExtension


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


The Charts
Calls
 sRet = GetExtension(sFile)
Call 1 sFile = "C:\Programs\Microsoft Office\Office\Winword.exe"
Call 2 sFile = "Winword.exe"
 VB5
CodeAuthorDopingNotes
GetExtension01 Donald  
GetExtension02 Donald  
GetExtension03Peter VB6
GetExtension04Peter VB6
GetExtension05 PeterFSO 
GetExtension06 ChrisAPI 
Call 1
44.505.604µs
22.392.973µs
   
   
32.903.607µs
11.001.245µs
Call 2
44.495.606µs
32.022.524µs
   
   
21.832.288µs
11.001.248µs
 VB6
CodeAuthorDopingNotes
GetExtension01 Donald  
GetExtension02 Donald  
GetExtension03 Peter VB6
GetExtension04 Peter VB6
GetExtension05 PeterFSO 
GetExtension06 ChrisAPI 
Call 1
64.465.714µs
32.142.745µs
53.454.430µs
22.012.577µs
42.763.543µs
11.001.283µs
Call 2
64.475.732µs
31.822.334µs
53.364.300µs
42.012.576µs
21.722.204µs
11.001.281µs
Conclusions
Comparing 01/02: minimizing your calls to VB string functions gets you on the speedway.
Comparing 03/04: VB6's InStrRev isn't necessarily the best choice when looking for a last occurrence.
The FileSystemObject can't convince when paths are given.
Mail your code! How to read all those numbers


The Code
GetExtension01
submitted 10-Oct-2000 by Donald Lessau  
Doping: none
Public Function GetExtension01(sFile As String) As String
' by Donald, donald@xbeat.net, 20001010
' the fastest you can get using Asc and Mid$, but still bad
  Dim iPos As Long
  
  For iPos = LenB(sFile) To 1 Step -2
    Select Case AscW(MidB$(sFile, iPos - 1))
    Case 46
      GetExtension01 = Mid$(sFile, iPos \ 2 + 1)
      Exit Function
    Case 92
      ' dir extensions don't count
      Exit Function
    End Select
  Next
  
End Function
Author's comments:
Donald's comments:

top | charts


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

top | charts


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

top | charts


GetExtension04
submitted 21-Oct-2000 by Peter Weighill  
Doping: none
Public Function GetExtension04(sFile As String) As String
' by Peter Weighill, pweighill@btinternet.com, 20001021
' Only for VB6
  Dim iPos As Long

  ' search last dot
  iPos = InStrRev(sFile, ".", -1, vbBinaryCompare)

  If iPos > 0 Then
    If InStr(iPos + 1, sFile, "\", vbBinaryCompare) = 0 Then
      GetExtension04 = Mid$(sFile, iPos + 1)
    End If
  End If
End Function
Author's comments: Quicker than GetExtension03: I noticed that InStrRev takes 3 times longer than InStr.
Donald's comments:

top | charts


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

top | charts


GetExtension06
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