I'm trying to make a directory listing of my drive here with the directories, subdirectories/folders, and files included. It also shows the file name, file path, date, and size. I'm currently using this code:
Dim iRow
Sub ListFiles()
iRow = 11 'starting row listing
Call ListMyFiles(Range("B5"), Range("B6"))
End Sub
Sub ListMyFiles(mySourcePath, IncludeSubfolders)
Set MyObject = New Scripting.FileSystemObject
Set mySource = MyObject.GetFolder(mySourcePath)
On Error Resume Next
For Each myFile In mySource.Files
iCol = 3
Cells(iRow, iCol).Value = myFile.Path
iCol = iCol + 1
Cells(iRow, iCol).Value = myFile.Name
iCol = iCol + 1
Cells(iRow, iCol).Value = myFile.Size
iCol = iCol + 1
Cells(iRow, iCol).Value = myFile.DateLastModified
iRow = iRow + 1
Next
If IncludeSubfolders Then
For Each mySubFolder In mySource.SubFolders
Call ListMyFiles(mySource.Path, True)
Next
End If
End Sub
Whenever I input an address it works fine, but when I decide to include the subfolders, it goes into haywire! It just loops on and on forever. It just does not stop. I don't know where to look for the error. I keep on rereading it and it seems okay, but maybe that's just me. Also, I ticked the Microsoft Scripting Runtime, for the record. And also, I think I still lack something here because I also wanted to show the Folder Names as well, if that is possible.
This question may not be clear or may be already duplicated, so let me know. Thank you!
P.S. Kind of a newbie here.
The path value that is being passed to the function for the sub folder listing is wrong.
It should be:
Call ListMyFiles(mySubFolder.Path, True)