Isaac
Lifelong Learner
- Local time
- Yesterday, 17:29
- Joined
- Mar 14, 2017
- Messages
- 9,910
So I'm using some code I cobbled together off of the web (I cannot remember from where any more), and you will note that the goal here is to START at one particular directory, and traverse all files in all subfolders (to each one's lowest level), listing each filename on a worksheet.
Now I know from using DIR to populate ONE folder's files (on a listbox which I did this once recently), it is VERY fast......like really astonishingly fast, even if the folder has 3000 files, DIR can populate them to an array in a moment.
I cannot figure out why this code, which starts at a top-level folder which does probably have a *total* of ... maybe 500 subfolders, and in some of them only a handful of files (5-10) and in others, maybe a couple hundred.........Why it is still running after half an hour!
Now it could be "correct" - but if you can spot any way to optimize this let me know? Thanks !!!
I wrote a small sub to create 1) a test folder to start with, and 2) about 80 levels of subfolders, each one with one file.
Then I ran this code on THAT structure, and it took maybe about 10 seconds to list out my 80-ish files.
Just wondering if anyone can see any part of my code that could be optimized - or rewritten differently - if the goal is to choose one top level folder and list out every file contained in any subfolder, going as deep as needed in all subfolders etc.
Excel, Access, I don't care - either is fine.
Now I know from using DIR to populate ONE folder's files (on a listbox which I did this once recently), it is VERY fast......like really astonishingly fast, even if the folder has 3000 files, DIR can populate them to an array in a moment.
I cannot figure out why this code, which starts at a top-level folder which does probably have a *total* of ... maybe 500 subfolders, and in some of them only a handful of files (5-10) and in others, maybe a couple hundred.........Why it is still running after half an hour!
Now it could be "correct" - but if you can spot any way to optimize this let me know? Thanks !!!
I wrote a small sub to create 1) a test folder to start with, and 2) about 80 levels of subfolders, each one with one file.
Then I ran this code on THAT structure, and it took maybe about 10 seconds to list out my 80-ish files.
Just wondering if anyone can see any part of my code that could be optimized - or rewritten differently - if the goal is to choose one top level folder and list out every file contained in any subfolder, going as deep as needed in all subfolders etc.
Excel, Access, I don't care - either is fine.
Code:
Sub LoopAllSubFolders(ByVal folderPath As String)
Dim fileName As String
Dim fullFilePath As String
Dim numFolders As Long
Dim folders() As String
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("FilesListing")
Dim i As Long
If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
fileName = Dir(folderPath & "*.*", vbDirectory)
While Len(fileName) <> 0
If Left(fileName, 1) <> "." Then
fullFilePath = folderPath & fileName
If (GetAttr(fullFilePath) And vbDirectory) = vbDirectory Then
ReDim Preserve folders(0 To numFolders) As String
folders(numFolders) = fullFilePath
numFolders = numFolders + 1
Else
'Insert the actions to be performed on each file
'This example will print the full file path to the immediate window
'Debug.Print folderPath & fileName
ws.Range("a" & ws.Rows.Count).End(xlUp).Offset(1, 0).Value = folderPath & fileName
'ws.Range("b" & ws.Rows.Count).End(xlUp).Offset(1, 0).Value = FileLen(folderPath & fileName) / 1000 'megabytes
End If
End If
fileName = Dir()
Wend
For i = 0 To numFolders - 1
LoopAllSubFolders folders(i)
Next i
End Sub
Sub loopAllSubFolderSelectStartDirectory()
Dim wb As Workbook, wsNew As Worksheet, strWorksheetName As String
ThisWorkbook.Worksheets("FilesListing").Range("xfd2:xfd60000").ClearContents
Call LoopAllSubFolders(frmMenu.txtFolderPath.Value)
End Sub