?
Solved

How to get the file-extension from a file?

Posted on 1998-12-19
7
Medium Priority
?
154 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
Industry Leaders: 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

764 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