dcsimg
Results 1 to 12 of 12
  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    62

    Filename without path and extention.

    I have this from a earlier question.

    With CommonDialog1
    .CancelError = True
    .InitDir = "C:\test\"
    .Filter = "All files (*.*)|*.*|Text (*.txt)|*.txt"
    .ShowOpen
    Text1.Text = .FileName
    End With

    It shows the file path and extention.

    Bud how can i get the only file name without the path and extention
    Example
    c:\test\visual Basic.exe

    And in Text2.Text i want Visual basic

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,197

    Re: Filename without path and extention.

    I don't use the CommonDialog control (using API calls instead), but I often find myself in the situation of wanting to get a file's name and extension from the complete specification (including path). I typically use InstrRev to do it, searching for "\" and take what's to the right using Mid$.

    Take Care,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    62

    Re: Filename without path and extention.

    I don't know how to do that.

    I'll try to learn and understand it.
    And ask al lot of questions after i have search on the internet without finding it.

    Bud the part i have i like..
    It workt fine for me.
    And i undertsand it

    Bun not how to get only the file name?

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,197

    Re: Filename without path and extention.

    Code:
    
        sFileSpec = "C:\Users\Elroy\Desktop\temp.docx"
        sFileName = Mid$(sFileSpec, InStrRev(sFileSpec, "\") + 1)
        MsgBox sFileName
    
    
    Enjoy,
    Elroy

    EDIT1: And here it is without the extension:

    Code:
    
        Dim sFileSpec As String
        Dim sFileName As String
        Dim sFileNameNoExt As String
    
        sFileSpec = "C:\Users\Elroy\Desktop\temp.docx"
        sFileName = Mid$(sFileSpec, InStrRev(sFileSpec, "\") + 1)
        sFileNameNoExt = Left$(sFileName, InStrRev(sFileName, ".") - 1)
    
    
        MsgBox sFileNameNoExt
    
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    62

    Re: Filename without path and extention.

    Yes... many thanks.
    I have hours search on the forums and tried many..
    Bud this one works..

    Thank you

  6. #6
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,137

    Re: Filename without path and extention.

    ... or try

    MsgBox Dir$("C:\Users\Elroy\Desktop\temp.docx")

    note: The specified file must exist, if not an empty string is returned.

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,197

    Re: Filename without path and extention.

    Magic, you know better than that. We don't want to have him doing disk I/O just to find his filename.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    62

    Re: Filename without path and extention.

    The solution from Elroy workt fine for me.
    Bud, magic, also thanks..

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,197

    Re: Filename without path and extention.

    Or, if we just want to be obscure, we could go:

    Code:
    
        Dim sFileSpec As String
        Dim sFileName As String
        Dim sFileNameNoExt As String
    
        sFileSpec = "C:\Users\Elroy\Desktop\temp.docx"
        sFileName = Split(sFileSpec, "\")(UBound(Split(sFileSpec, "\")))
        sFileNameNoExt = Split(sFileName, ".")(UBound(Split(sFileName, ".")) - 1)
    
    
        MsgBox sFileNameNoExt
    


    How many ways to skin a cat.

    EDIT1: If we worked at it, I bet we could come up with some way to use recursion to do it.

    EDIT2: Here we go:

    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
    
    
        Dim sFileSpec As String
        Dim sFileNameNoExt As String
    
        sFileSpec = "C:\Users\Elroy\Desktop\temp.docx"
    
        MsgBox sFileName(sFileSpec)
        MsgBox sTrimExt(sFileName(sFileSpec))
    
    
    End Sub
    
    
    Private Function sFileName(ByVal sFileSpec As String) As String
        If InStr(sFileSpec, "\") = 0 Then
            sFileName = sFileSpec
            Exit Function
        End If
        sFileName = sFileName(Mid$(sFileSpec, 2))
    End Function
    
    Private Function sTrimExt(ByVal sFileName As String) As String
        If Right$(sFileName, 1) = "." Then
            sTrimExt = Left$(sFileName, Len(sFileName) - 1)
            Exit Function
        End If
        sTrimExt = sTrimExt(Left$(sFileName, Len(sFileName) - 1))
    End Function
    
    Last edited by Elroy; Feb 14th, 2018 at 02:00 PM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  10. #10
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,137

    Re: Filename without path and extention.

    > We don't want to have him doing disk I/O just to find his filename

    Makes few odds really because he just did disk I/O to get the Common Dialog showing files in the selected folder, so the stuff required by the call to Dir$ is almost certainly already handy in the disk cache.

    Just an alternative ...

  11. #11
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,197

    Re: Filename without path and extention.

    Quote Originally Posted by Magic Ink View Post
    > We don't want to have him doing disk I/O just to find his filename

    Makes few odds really because he just did disk I/O to get the Common Dialog showing files in the selected folder, so the stuff required by the call to Dir$ is almost certainly already handy in the disk cache.

    Just an alternative ...
    You're right.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  12. #12
    PowerPoster
    Join Date
    Oct 2013
    Posts
    3,103

    Re: Filename without path and extention.

    The .FileTitle property returns the filename without the path:
    https://msdn.microsoft.com/en-us/lib...(v=vs.60).aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.