Get all File Names from a Folder
1 min readJan 16, 2022
Code for Only Getting Names:
Sub LoopThroughFiles()
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim i As Integer
Set oFSO = CreateObject("Scripting.FileSystemObject")
FolderName = Range("B2").Value
Set oFolder = oFSO.GetFolder(FolderName)
For Each oFile In oFolder.Files
Cells(i + 1, 1) = oFile.Name
i = i + 1
Next oFile
End Sub
Code for Getting Names with Link:
Sub LoopThroughFilesWithLink()
Dim xRow As Long
Dim xDirect, xFname$, InitialFoldr$
InitialFoldr$ = "C:\" '<<< 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) & "\"
sn = Split(CreateObject("wscript.shell").exec("cmd /c dir " & xDirect & "/b/s").stdout.readall, vbCrLf)
i = 2
For Each d In sn
ActiveSheet.Hyperlinks.Add Cells(i, "A"), d
i = i + 1
Next d
End If
End With
End Sub
GitHub:
https://github.com/havishmad/excel_file_names_from_folder
YouTube:
https://youtu.be/Dn-l7SAFHig