Results 1 to 17 of 17

Thread: Extracting file extension

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Midwest
    Posts
    35

    Question

    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.

  2. #2
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    Take a look here: http://forums.vb-world.net/showthrea...threadid=27580

    It might give you a few ideas to extract the extension.


  3. #3
    Guest
    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

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Midwest
    Posts
    35

    Exclamation 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.

  5. #5
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    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]

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    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.

  7. #7

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Midwest
    Posts
    35

    Exclamation Another big thanks

    Another big thanks for all the help. I am truly appreciative!!!

  8. #8
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    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

  9. #9
    New Member
    Join Date
    Oct 2000
    Posts
    9
    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

  10. #10
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736

    Cool

    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 !

  11. #11
    Hyperactive Member gravyboy's Avatar
    Join Date
    Jan 2000
    Location
    Where I was before . . . if you don't know then you're new!
    Posts
    334

    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.
    Matt G
    VS6 Ent SP5 @ Work
    VS6 Ent SP5 & VB.Net @ Home
    [email protected]



  12. #12

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Midwest
    Posts
    35

    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,



  13. #13

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Midwest
    Posts
    35

    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)

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  15. #15

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Midwest
    Posts
    35

    ???

    Kedaman,
    Are there problems that could arise from using FSO? Please enlighten me.

  16. #16
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  17. #17

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Midwest
    Posts
    35

    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
  •  



Click Here to Expand Forum to Full Width