Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to get the file-extension from a file?

Posted on 1998-12-19
7
Medium Priority
?
156 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 200 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…
Suggested Courses

618 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