How to get the file-extension from a file?

What is the best way to get the file-extension from a filename?
I have made some code myself, but it is not perfect.
E.g. If I run "Autoexec.bat" through the code, it returns ".bat".
And if I pass "MyFile", I get ".".
It even works if I pass "A.File. with.periods.txt", then I get ".txt"

But the code is not fast enough, because I use a do until-loop function
to get the last period when the filename has got more the one.

So if someone could give me some fast code or a fast api to fix this...
Who is Participating?
vikiingConnect With a Mentor Commented:
Sub GetExtension(filename as String, Ext as string)
For i%=len(FileName) to 1 step -1
  If mid$(FileName,I%,1)="." then Ext=Mid$(Filename, I%) : Exit Sub
Next i%
End Sub

Instr function is performed through a SCANB machine-instruction. Although SCANB can be directed to do a backwards search, a function like "RevInstr", unfortunately, does not exist... :~(

Here is some code that should work:

Sub GetExtention(filename as String)
  Position = InStr(1, filename, ".")
  ExtentionX = Mid(filename, Position)
End Sub

You should also declare ExtentionX in the Declarations so you can retreive the string.
Dear friend TomKid: your algorithm doesn't work good, because of this: you simply look for the position of the FIRST period existing into filename, but keep in mind that:
a) Filename can include a path, and the name of a sub-directory can include a period.
b) Besides that, long filenames can include periods (among other special chars), like in "This is Mr.Jone's file.doc"

Here, extension is simplΓΏ ".DOC", but you're giving ".Jone's file.doc".

Although it's not *THE* solution, one can look for the period, but backwards:

Sub GetExtension(filename as String, Ext as string)
For i%=len(FileName) to 1 step -1
  If mid$(FileName,I%,1)="." then Ext=Mid$(Filename, I%) : Exit Sub
Next i%
End Sub

If FileName doesn't contain a period, Ext will be returned as null.

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Hey Olli,
  This is what I always use. It's simple, not too slow, and only gives you the last occurrence of an extension (even extensions longer than 3 letters such as "html"):

Public Function StripExtension(strFName As String) As String
'* Strip the extension from a filename.extension
    Dim chr As String * 1
    Dim intX As Integer
    Dim intNameLength As Integer
    chr = ""
    intX = Len(strFName)
    intNameLength = intX
    While chr <> "." And intX <> 0
        chr = Mid$(strFName, intX, 1)
        intX = intX - 1
    If intX = 0 Then
        StripExtension = ""
        intX = intX + 1
        StripExtension = Right$(strFName, intNameLength - intX)
    End If
End Function

Don't know if this is fast enough for what you want, but it should do the trick...

I was just thinking....wouldn't it be easier to just use a Left() command to get back three/four spaces and get the extention that way? Of course it wouldn't be very useful for very long extensions....
Friend Nkoss: I supose you mean Right$() function (Left$() brings you the left portion).

Anyway, even if you use Right$(), to be sure to get only the extension, you must look for the period; you can't "guess" where is it thinking *ALL* files have their extensions with 3 letters.

Of course, they are a big majority; but files like .C , .H , .NG , .TP and others (like READ.ME) are a lot too.

Olli083097Author Commented:
As vikiing said, the code is to good...

vikiing, CyberGar:
Both your codes works fine, but as I said I need it to be fast.
This is because I call it extreamly often. So what I did is that I made a code-speed-tester application. And the test shows that vikiing's code is faster...

If you would like the points, then answer the question!

Thanx to all of you!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.