VBA:Get File Names Excluded Extension Name of File

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

VBA:Get File Names Excluded Extension Name of File

Post by Susanto3311 »

hi all.
this below code working properly to get list name file from a folder include extension name of file
but i want someone would to modified this code can pull name list from a folder without any extension name of file
here code

Code: Select all

Sub GetFileNames() 
    Dim xRow As Long
    Dim xDirect$, xFname$, InitialFoldr$
     
    InitialFoldr$ = "G:\" '<<< Startup folder to begin searching from
     
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please select a folder to list Files from"
        .InitialFileName = InitialFoldr$
        .Show
        If .SelectedItems.Count <> 0 Then
            xDirect$ = .SelectedItems(1) & "\"
            xFname$ = Dir(xDirect$, 7)
            Do While xFname$ <> ""
                ActiveCell.Offset(xRow) = xFname$
                xRow = xRow + 1
                xFname$ = Dir
            Loop
        End If
    End With
End Sub
anyone help me, greatly appreciated
susanto

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA:Get File Names Excluded Extension Name of File

Post by HansV »

Does this do what you want?

Code: Select all

Sub GetFileNames()
    Dim xRow As Long
    Dim xDirect$, xFname$, InitialFoldr$
     
    InitialFoldr$ = "G:\" '<<< Startup folder to begin searching from
     
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please select a folder to list Files from"
        .InitialFileName = InitialFoldr$
        .Show
        If .SelectedItems.Count <> 0 Then
            xDirect$ = .SelectedItems(1) & "\"
            xFname$ = Dir(xDirect$, 7)
            Do While xFname$ <> ""
                ActiveCell.Offset(xRow) = Left$(xFname$, InStrRev(xFname$, ".") - 1)
                xRow = xRow + 1
                xFname$ = Dir
            Loop
        End If
    End With
End Sub
Best wishes,
Hans

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

Re: VBA:Get File Names Excluded Extension Name of File

Post by Susanto3311 »

hi Hans, thank you so much!! Working well.