Consulting

Results 1 to 4 of 4

Thread: VBA: USE InStrRev to get file Name

  1. #1
    VBAX Regular
    Joined
    Nov 2018
    Location
    London, U.K.
    Posts
    99
    Location

    VBA: USE InStrRev to get file Name

    I am trying to get the filenames from a folder and place it in a textbox. I am using File Dialog Open to
    get the file path\filenames.
    Someone kindly helped me to get the directory using InStrRev:

    n = InStrRev(fDialog.SelectedItems(1), "\")
     TextBox2.Value = Left(fDialog.SelectedItems(1), n)

    I want the filename instead, so I found


    varFileName = Mid(fDialog.SelectedItems(1), InStrRev(fDialog.SelectedItems(1), "\") + 1, Len(fDialog.SelectedItems(1)))

    I don't know how to put all of this together to get the file name into TextBox2.

    Any help would be appreciated.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    These are the pieces using VBA

    Option Explicit
    Sub GetFileName()
        Dim s As String, s1 As String
        Dim i As Long
        
        s = "C:\Users\Me\Documents\Latest File.xlsx"
        
        i = InStrRev(s, "\")
        
        s1 = Right(s, Len(s) - i)
        MsgBox s1
        
    End Sub

    or using the FileSystemObject

    Sub GetFileName2()
        Dim s As String, s1 As String
        
        s = "C:\Users\Me\Documents\Latest File.xlsx"
        
        s1 = CreateObject("Scripting.FileSystemObject").GetFileName(s)
        MsgBox s1
        
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Nov 2018
    Location
    London, U.K.
    Posts
    99
    Location
    Thank you for this. How would this code change if there is more than 1 file in the folder (which may be the case).? Ideally the macro would get the names of all files.

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Sub DirStdOut()  
      Dim s As String, a() As String
      ' /b = bare file listing, /s = search subfolders, /c = open command shell, run command and then close shell.
      ' /a:-d means list files only and not subfolders
      s = CreateObject("Wscript.Shell").Exec("cmd /c dir C:\myfiles\wp\Klaus\*.* /a:-d /b").StdOut.ReadAll
      a() = Split(s, vbCrLf)
      With Range("A1")
        .EntireColumn.Clear
        .Resize(UBound(a)).Value = WorksheetFunction.Transpose(a)
      End With
    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
  •