Results 1 to 17 of 17

Thread: Strip file name (in a string) of extention

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Posts
    142
    How would I strip a filename (in a string) of its extention.

    eg
    filestr="Textfile.txt"

    I want filestr to = "Textfile"

    Thanks

    Garrett

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    couple ways

    x = instr(filestr,".")
    filestr = left(filestr,x-1)


    dim tmp() as string
    tmp = SPlit(filestr,".")
    filestr = tmp(0)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    Addicted Member
    Join Date
    Jan 2001
    Location
    California
    Posts
    203

    This should work

    dim intPosition as integer
    dim newString as string

    intPosition = inStr(1, textfile.txt, ".")
    NewString = left(textfile.txt, intPosition - 1)

    NewString should end up = to txtfile

  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Or..
    Code:
    Private Sub Command1_Click()
        x = InStrRev(Text1, ".")
        z = Mid(Text1, 1, x - 1)
        Print z
    End Sub

  5. #5
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    ok...

    for x = 1 to len(filestr)
    i = mid(filestr,x,1)
    if i = "." then exit for
    tmp = tmp & i
    next x

    filestr = tmp




    gotta cover all possibilities right?
    Last edited by Static; Feb 7th, 2001 at 01:37 PM.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  6. #6
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    Or, one more option.

    Code:
    Private Sub Command1_Click()
        Dim fs As Object
        Set fs = CreateObject("Scripting.FileSystemObject")
        MsgBox fs.GetBaseName("c:/extension.txt")
    End Sub

  7. #7
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    what if there are multiple "." in a file....?


    for x = 0 to len(filestr)
    i = mid(filestr,x,1)
    if i = "." then exit for
    tmp = tmp & i
    next x

    filestr = tmp
    should be......

    Code:
    For x = len(filestr) to 0 step -1
         i = mid(filestr,x,1)
         if i = "." then exit for
         tmp = tmp & i
    Next x
    
    filestr = tmp

    :P
    Bababooey
    Tatatoothy
    Mamamonkey

  8. #8
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    You code print "txt", he wants the other way around.

  9. #9
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    good point...

    if there are multiple "."'s....

    jbart's code would be best....

    just change it to take the variable


    Code:
    Private Sub Command1_Click()
        Dim fs As Object
        Set fs = CreateObject("Scripting.FileSystemObject")
        MsgBox fs.GetBaseName(filestr)
    End Sub
    good thinking jbart
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  10. #10
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    neat piece of code, i learned something new :P

    btw, how come vb doesn't autocomplete those properties
    for me? I'm referencing the microsoft scripting runtime.

    Is there anything else i need to reference?
    Bababooey
    Tatatoothy
    Mamamonkey

  11. #11
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    It's because your using "CreateObject", if you use the syntax, you will get the IntelliSense.
    Code:
    Dim fso as FileSystemObject
    Set fso = New FileSystemObject
    fso.GetBaseName

  12. #12
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    when you use creatobject...VB doesnt know what the Object will be until it is run...so it does not recognize the object name.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  13. #13
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    yep, Late Binding

  14. #14
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    ty
    Bababooey
    Tatatoothy
    Mamamonkey

  15. #15
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    Thanks Lethal. I have been blindly using the code as it appears in the MSDN examples for the FileSystemObject.

    With your way I get to see all the options.

  16. #16
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Hey no prob, we both learned something new

  17. #17
    Guest
    Just sharing more code of how to get the filename and not the extension.


    Code:
    Private Sub Command1_Click()
    
        Dim sFile As String
        Dim vArray As Variant
        Dim uArray As Integer
        
        sFile = "C:\Textfile.txt"
        vArray = Split(sFile, ".")
        uArray = LBound(vArray)
    
        MsgBox vArray(uArray)
    
    End Sub

    If you wish to get the extension and not the filename...


    Code:
    Private Sub Command1_Click()
    
        Dim sFile As String
        Dim vArray As Variant
        Dim uArray As Integer
        
        sFile = "C:\Textfile.txt"
        vArray = Split(sFile, ".")
        uArray = UBound(vArray)
    
        MsgBox Chr$(46) & vArray(uArray)
    
    End Sub

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