Solved

Capture file extention

Posted on 2007-12-07
6
195 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:Abiel de Groot
6 Comments
 
LVL 19

Expert Comment

by:Delphineous Silverwing
ID: 20426801
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
 
LVL 18

Expert Comment

by:Morcalavin
ID: 20426833
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
 
LVL 5

Author Comment

by:Abiel de Groot
ID: 20427000
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 250 total points
ID: 20427040
Dim WordArray

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

WordArray = Split(NameStr, ". ")

WordArray(1)   , you will get extention
0
 
LVL 18

Assisted Solution

by:Morcalavin
Morcalavin earned 250 total points
ID: 20427287
@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
 
LVL 5

Author Comment

by:Abiel de Groot
ID: 20427489
Almost right...
WordArray(Ubound(WordArray) - 1) returns the file name...

WordArray(Ubound(WordArray)) returns the extension


Kind regards to all

Abbey
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

810 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