|
-
Oct 9th, 2000, 01:25 PM
#1
Thread Starter
Member
How do you extract the file extensions from filenames (ie. doc, xls, ppt, etc.). I would like to use getExtension command but I am having trouble with the proper syntax. If you know how to use getExtension or a better way please let me know. Thanks.
-
Oct 9th, 2000, 01:41 PM
#2
Fanatic Member
Take a look here: http://forums.vb-world.net/showthrea...threadid=27580
It might give you a few ideas to extract the extension.
-
Oct 9th, 2000, 01:42 PM
#3
This will retrieve a file extension from the given file path.
Code:
Private Function Extension(ByVal strFilename As String) As String
Dim intPos As Integer
Extension = vbNullString
intPos = Len(strFilename)
Do While intPos > 0
Select Case Mid$(strFilename, intPos, 1)
Case "."
Extension = Mid$(strFilename, intPos + 1)
Exit Do
Case Else
End Select
intPos = intPos - 1
Loop
End Function
-
Oct 9th, 2000, 02:14 PM
#4
Thread Starter
Member
Thanks!
Thanks for your assistance. I have a question, is there a command in VB6 that will allow you to do this without creating a function or subroutine.
-
Oct 10th, 2000, 08:18 AM
#5
Fanatic Member
You could use the InStr function to locate the period, then use the Mid function to extract the extension.
Code:
Private Sub Command1_Click()
Dim periodPosition As Integer
Dim extensionValue As String
periodPosition = InStr(Text1.Text, ".") + 1
extensionValue = Mid$(Text1.Text, periodPosition)
MsgBox extensionValue
End Sub
or combine the Functions on one line.
Code:
Private Sub Command1_Click()
Dim extensionValue As String
extensionValue = Mid$(Text1.Text, InStr(Text1.Text, ".") + 1)
MsgBox extensionValue
End Sub
[Edited by jbart on 10-10-2000 at 09:20 AM]
-
Oct 10th, 2000, 08:38 AM
#6
transcendental analytic
Fast code :)
Code:
Function Extension(Filename As String) As String
Dim a() As Byte, b&
a = StrConv(Filename, vbUnicode)
For b = UBound(a) To 0 Step -1
If a(b) = 46 Then Extension = Mid(Filename, b): Exit Function
Next b
End Function
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 10th, 2000, 06:56 PM
#7
Thread Starter
Member
Another big thanks
Another big thanks for all the help. I am truly appreciative!!!
-
Oct 11th, 2000, 06:50 AM
#8
Fanatic Member
Even though you have received several great ways to get the extension, your original post mentioned using the GetExtension command. The code below does that using the FileSystemObject.
Code:
Private Sub Command1_Click()
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
MsgBox fs.GetExtensionName("c:/extension.txt")
End Sub
-
Oct 11th, 2000, 06:55 AM
#9
New Member
Or to be a padantic pain in the arse,
In two lines
Code:
Dim fs as New Scripting.FileSystemObject
MsgBox fs.GetExtensionName("c:/extension.txt")
Terry
-
Oct 11th, 2000, 07:00 AM
#10
Fanatic Member
Terry,
When I run that code I get an error "User-defined type not defined". Is there a component or reference that needs to be added to the project?
Thanks !
-
Oct 11th, 2000, 07:13 AM
#11
Hyperactive Member
Longfilenames
You can't search for a full-stop/period as they can be numerous given than Windoze from 95 on allowed long filenames.
Hmm... Wasn't that how the ILOVEYOU and LOVEBUG got quickly passed around? Windoze by default hides the extension when it is known - in this case it only showed the .txt extension and the rest is history (the .VBS ext was recognized 'cos Outlook allows scripting)
I apologize for not responding with an answer.
-
Oct 11th, 2000, 12:53 PM
#12
Thread Starter
Member
Tried but no luck
I tried the following code:
--------------------------------------------------------------------------------
Dim fs as New Scripting.FileSystemObject
MsgBox fs.GetExtensionName("c:/extension.txt")
--------------------------------------------------------------------------------
I got the same error "User-defined type not defined". Is there something missing that I need to add?
Thanks again,

-
Oct 11th, 2000, 01:00 PM
#13
Thread Starter
Member
Answer provided
I received an answer to the problem from a well versed VB developer (surfmstr), see below:
You need to add a reference to MS Scripting Runtime
and then it will work.
Thanks Surfmstr (aka - Rob)
-
Oct 12th, 2000, 06:05 AM
#14
transcendental analytic
Hey don't use FSO for such an easy task
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 12th, 2000, 06:20 AM
#15
Thread Starter
Member
???
Kedaman,
Are there problems that could arise from using FSO? Please enlighten me.
-
Oct 12th, 2000, 07:09 AM
#16
transcendental analytic
Only technically, i mean you have one more file to include in your apps dependencies, and using vb you already need to have the runtimes... 
But also, it's slower although you won't notice anything unless the process is very frequent.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 12th, 2000, 08:48 AM
#17
Thread Starter
Member
Thanks!
I appreciate the feedback Kedaman.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|