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.
Printable View
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.
Take a look here: http://forums.vb-world.net/showthrea...threadid=27580
It might give you a few ideas to extract the extension.
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
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.
You could use the InStr function to locate the period, then use the Mid function to extract the extension.
or combine the Functions on one line.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
[Edited by jbart on 10-10-2000 at 09:20 AM]Code:Private Sub Command1_Click()
Dim extensionValue As String
extensionValue = Mid$(Text1.Text, InStr(Text1.Text, ".") + 1)
MsgBox extensionValue
End Sub
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
Another big thanks for all the help. I am truly appreciative!!!
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
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
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 !
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.
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,
;)
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)
;)
Hey don't use FSO for such an easy task ;)
Kedaman,
Are there problems that could arise from using FSO? Please enlighten me.
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.
I appreciate the feedback Kedaman.