Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looking at file extentions

Status
Not open for further replies.

gadget3302

Programmer
Apr 24, 2003
79
US
I what to use an If then statement to determine whether a file extention is usable. More specifically I have saved a file name into a String variable and what to read the last 3 characters to determine if they are correct. I'm new at programming and I have a VB book on the way, but your advice would be appreaciated.

Thanks
 
To get the last 3 characters of a string:

Dim FileExt As String

FileExt = Mid(FileNameStr, Len(FileNameStr) - 2)

Note: use your string variable name in place of FileNameStr.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
The extensions are not only 3 letters, eg myfile.class (class file in java) or in asp.net "default.aspx" !!

So, you will need to find the last dot at the filename.
-reverse the string : strreverse(...)
-find "." : instr("string", ".")

....
 
Take a look at this thread.

thread222-811196


Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
Here's another solution you may want to try.

Dim FSO As Scripting.FileSystemObject

Set FSO = CreateObject("Scripting.FileSystemObject")
MsgBox FSO.GetExtensionName("C:\My\Folder\MyFile.MyExtension")
Set FSO = Nothing

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Or how about:

Code:
Dim sExtension As String
Dim sFileName As String
Dim iDotLocation As Integer

sFileName = "file.name"
iDotLocation = InStr(1, sFileName, ".")
sExtension = Right(sFileName, Len(sFileName) - iDotLocation)

 
The problem with that, kodr, is that there is no law that says that there should only be one period in a filename
 
Try opening up word and saving a file using an extension different from the one currently in the drop down menu.
 
For Example: your file name is "abc.efe.ccc.BAT"

myItems = Split(myItemsData, ".")
i=UBound(myItems)
extention = myItems(i)

The only problem with this is the file name without extention.
 
Well, how about this one?

Code:
Dim sExtension As String
Dim sFileName As String
Dim iDotLocation As Integer

sFileName = "file.name"
iDotLocation = InStr(1, sFileName, ".")
sExtension = Right(sFileName, Len(sFileName) - iDotLocation)


You could add the following to check for the condition that not period exists in the file name.

Code:
if iDotLocation = 0 then .....
 
Oops, should have been:

Code:
Dim sExtension As String
Dim sFileName As String
Dim iDotLocation As Integer

sFileName = "file.name"
iDotLocation = InStrRev(sFileName, ".")
sExtension = Right(sFileName, Len(sFileName) - iDotLocation)
 
Thanks so much everyone. Kodr it worked like a charm thanks.
 
wangdong, has an interesting point although I vote for gmmastros way since there are many to none [red].[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top