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