Consulting

Results 1 to 4 of 4

Thread: VBA functionality for String manipulation

  1. #1

    VBA functionality for String manipulation

    I am given a complete path of a file like "c:/folder1/subfolder-2/.../subfolder-n/xyz.xls". Now from this string, I need to extract only the file name i.e. 'xyz.xls'.

    Is there any VBA function to accomplish that?

    Thanks for your help.

  2. #2
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    Parse the string with SPLIT():

    Sub dural()
    Dim s As String
    s = "C:\bigfolder\mediumfolder\smallfolder\tinyfolder\microfolder\temp.xls"
    ary = Split(s, "\")
    MsgBox ary(UBound(ary))
    End Sub
    Last edited by Aussiebear; 12-20-2022 at 12:07 PM. Reason: Added code tags
    Have a Great Day!

  3. #3
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    One more:
    Public Sub GetFileName()
    Dim strPath As String, strFile As String
    strPath = "c:/folder1/subfolder-2/.../subfolder-n/xyz.xls"
    strFile = Mid(strPath, InStrRev(strPath, "/") + 1, Len(strPath))
    MsgBox strFile
    End Sub
    Last edited by Aussiebear; 12-20-2022 at 12:08 PM. Reason: Edited code tags
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    You must be kidding:

     
    msgbox dir("c:/folder1/subfolder-2/.../subfolder-n/xyz.xls")
    Your first lesson/chapter in VBA manipulating files/folders.

    See also:

    http://www.snb-vba.eu/VBA_Bestanden_en.html
    Last edited by Aussiebear; 12-20-2022 at 12:08 PM. Reason: Edited code tags

Posting Permissions

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