/build/static/layout/Breadcrumb_cap_w.png

Names of all the files in a folder Excluding Sub folder

If you want to get the names of all the files stored in a folder and excluding the files stored in a sub folder.Try below code-


Option Explicit
Sub file_names_in_folder_without_including_files_in_subfolder()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim fldpath
Dim fld As Object, fil As Object, fso As Object, j As Long
    With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Choose the folder"
    .Show
    End With
    On Error Resume Next
    fldpath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1) & "\"
    If fldpath = False Then
        MsgBox "Folder Not Selected"
    Else
    Workbooks.Add
    Cells(1, 1).Value = fldpath
    Cells(3, 1).Value = "Path"
    Cells(3, 2).Value = "Dir"
    Cells(3, 3).Value = "Name"
    Cells(3, 4).Value = "Size"
    Cells(3, 5).Value = "Type"
    Cells(3, 6).Value = "Date Created"
    Cells(3, 7).Value = "Date Last Access"
    Cells(3, 8).Value = "Date Last Modified"
    j = 4
    Set fso = CreateObject("scripting.filesystemobject")
    Set fld = fso.getfolder(fldpath)
    For Each fil In fld.Files
        Cells(j, 1).Value = fil.Path
        Cells(j, 2).Value = Left(fil.Path, InStrRev(fil.Path, "\"))
        Cells(j, 3).Value = fil.Name
        Cells(j, 4).Value = fil.Size
        Cells(j, 5).Value = fil.Type
        Cells(j, 6).Value = fil.DateCreated
        Cells(j, 7).Value = fil.DateLastAccessed
        Cells(j, 8).Value = fil.DateLastModified
        j = j + 1
    Next
    End If
    Range("a1").Font.Size = 9
    ActiveWindow.DisplayGridlines = False
    Range("a4:h" & Range("a4").End(xlDown).Row).Font.Size = 9
    Range("a3:h3").Interior.Color = vbCyan
    Columns("c:h").AutoFit
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub


Comments

This post is locked
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ