Solved

How to get the file-extension from a file?

Posted on 1998-12-19
7
143 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
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now