Solved

Capture file extention

Posted on 2007-12-07
6
192 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 250 total points
Comment Utility
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
Comment Utility
@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
Comment Utility
Almost right...
WordArray(Ubound(WordArray) - 1) returns the file name...

WordArray(Ubound(WordArray)) returns the extension


Kind regards to all

Abbey
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

10 Experts available now in Live!

Get 1:1 Help Now