Solved

How to get the file-extension from a file?

Posted on 1998-12-19
7
148 Views
Last Modified: 2012-05-04
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...
0
Comment
Question by:Olli083097
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Expert Comment

by:TomKid2004
ID: 1451504
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.
0
 
LVL 3

Expert Comment

by:vikiing
ID: 1451505
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)
Ext=""
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.



0
 
LVL 1

Expert Comment

by:CyberGar
ID: 1451506
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
    Wend
    If intX = 0 Then
        StripExtension = ""
    Else
        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...

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:nkoss
ID: 1451507
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....
0
 
LVL 3

Expert Comment

by:vikiing
ID: 1451508
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.

0
 

Author Comment

by:Olli083097
ID: 1451509
TomKid2004:
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...

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

Thanx to all of you!
0
 
LVL 3

Accepted Solution

by:
vikiing earned 50 total points
ID: 1451510
Sub GetExtension(filename as String, Ext as string)
Ext=""
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... :~(

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question