Capture file extention

I need to capture the file extension. I have collected the full filename and extension. I used to use;

------------------------------
strAtPos = Instr(checkFile,".")
strExtension = Mid(checkFile,strAtPos+1)
------------------------------

But this does not take account of people who use dots in their file names, Such as test.this.file.txt

I also want to avoid just grabbing the last 3 characters as some file types have 4... such as html or jpeg.

So in the example 'test.this.file.txt' what would be the best way to get the file extension allowing for various file types with various lenghts.


Kind regards

Abiel M de Groot
LVL 5
Abiel de GrootAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Pratima PharandeConnect With a Mentor Commented:
Dim WordArray

then do the split, using a space as the split indicator

WordArray = Split(NameStr, ". ")

WordArray(1)   , you will get extention
0
 
Delphineous SilverwingGood Ol' GeekCommented:
The extension is the last dot "." in the file name.  You can set the routine to loop until the last dot is found, or search from the right.
0
 
MorcalavinCommented:
You can try a RegExp to grab the last extension:

dim regex, ext, filename

filename = "test.1.2.txt"

set regex = new RegExp
regex.pattern = "\.(.*)+$"

ext = regex.match(filename)

ext should be an array containing the matches if I remember.  Been a while since I've done regexes in ASP.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Abiel de GrootAuthor Commented:
I have this but the server hangs... and I have to reboot!
checkFile Is the full file name with extension

Any Ideas?
strAtPos = Instr(checkFile,".")
 
Do Until strAtPos = 0
strAtPos = Instr(strAtPos,checkFile,".")	
Loop

Open in new window

0
 
MorcalavinConnect With a Mentor Commented:
@pratima_mcs

That would only grab the second place, ie if the string is file.1.txt you would end up with 1.

However, you could do something like:
Dim WordArray

WordArray = Split(NameStr, ".")
Response.Write WordArray(Ubound(WordArray) - 1)
0
 
Abiel de GrootAuthor Commented:
Almost right...
WordArray(Ubound(WordArray) - 1) returns the file name...

WordArray(Ubound(WordArray)) returns the extension


Kind regards to all

Abbey
0
All Courses

From novice to tech pro — start learning today.