I was reading several of the threads on listing subdirectories and the code struck me as perhaps more complicated than I wanted it to be.
So I wrote a non-recursive, 10-line subroutine that will create the listing (ok, there's also a couple of DIM lines, too, including a Global array where the results are stored).
This example lists subfolders only. Use the following line to change it to include normal files.
In my search I didn't see anyone using this particular algorithm to create a subdirectory listing.
As a minimalist kind of programmer, I like the compactness - not to mention how easy it is to understand.
On small directory structures it's very fast - basically instant response. On a full c-drive it took about 1 minute to read about 10,000 subfolders. I don't know enough about DIR$ to know whether the number of files matters in how fast it works, or if just the number of subfolders is the determining factor.
I keep meaning to copy one of the recursive versions and do a speed comparison.
Once the code is used, Window keeps all of the directory info in a cache, so doing tests means I have to reboot or, if I knew how, clear the cache to get valid answers. Does anyone know an easy way to clear the cache so subsequent runs of the code won't give wrong answers (basically instant response for an entire drive).
I don't do subfolder listings all that often so I've not given much thought on how to speed this up. I assume there is an API that DIR$ draws on. Would that speed this up significantly?
Below is a complete PowerBASIC app that puts the results into a listbox.
Note that I dimensioned the array to hold 10,000 subfolders. To list files, a much larger array is needed. Also, the subroutine returns the list unsorted. In this example, the listbox sorts it, but I could have used ARRAY SORT.
Another trick I used is to create the listbox after the list array was created. That way I can load an entire array of data in one statement. That brings up a question - can I load an array to a listbox, adding the array content to what is already in the listbox? Help doesn't say I can.
I can think of a few data verification things that would make it safer, like verifying that the starting folder is valid. Or, a separate array for file and for folders would also be convenient in some situations.
So I wrote a non-recursive, 10-line subroutine that will create the listing (ok, there's also a couple of DIM lines, too, including a Global array where the results are stored).
Code:
Sub ListSubFolders(startfolder$) 'Start$ cannot end in \ 'returns list of folder (full paths) in Folder() - unsorted Dim iPOS As Long Folder(FolderCount) = startfolder$ While Len(Folder(iPOS)) temp$ = Dir$(Build$(Folder(iPOS),"\*.*"), Only %SubDir) 'subfolders only While Len(temp$) Incr FolderCount Folder(FolderCount) = Build$(Folder(iPos),"\",temp$) temp$ = Dir$ (Next) Wend Incr iPOS Wend End Sub
Code:
temp$ = Dir$(Build$(Folder(iPOS),"\*.*"), %SubDir) 'subfolders + files
As a minimalist kind of programmer, I like the compactness - not to mention how easy it is to understand.
On small directory structures it's very fast - basically instant response. On a full c-drive it took about 1 minute to read about 10,000 subfolders. I don't know enough about DIR$ to know whether the number of files matters in how fast it works, or if just the number of subfolders is the determining factor.
I keep meaning to copy one of the recursive versions and do a speed comparison.
Once the code is used, Window keeps all of the directory info in a cache, so doing tests means I have to reboot or, if I knew how, clear the cache to get valid answers. Does anyone know an easy way to clear the cache so subsequent runs of the code won't give wrong answers (basically instant response for an entire drive).
I don't do subfolder listings all that often so I've not given much thought on how to speed this up. I assume there is an API that DIR$ draws on. Would that speed this up significantly?
Below is a complete PowerBASIC app that puts the results into a listbox.
Note that I dimensioned the array to hold 10,000 subfolders. To list files, a much larger array is needed. Also, the subroutine returns the list unsorted. In this example, the listbox sorts it, but I could have used ARRAY SORT.
Another trick I used is to create the listbox after the list array was created. That way I can load an entire array of data in one statement. That brings up a question - can I load an array to a listbox, adding the array content to what is already in the listbox? Help doesn't say I can.
Code:
#Compile Exe Global hDlg As Dword Global Folder() As String, FolderCount As Long Function PBMain() As Long Dialog New Pixels, 0, "ListBox Test",300,100,230,300, _ %WS_OverlappedWindow, 0 To hDlg Control Add Button, hDlg, 100,"Start Search", 10,10,100,20 Control Add Label, hDlg, 150,"", 120,10,100,20, %WS_Border Or %SS_Center Dialog Show Modal hDlg Call DlgProc End Function CallBack Function DlgProc() As Long If Cb.Msg = %WM_Command And Cb.Ctl = 100 And Cb.CtlMsg = %BN_Clicked Then Dim Folder(10000) ListSubFolders("d:\temp") ReDim Preserve Folder(FolderCount) Control Add ListBox, hDlg, 200, Folder(), 10,40,210,250 Control Set Text hDlg, 150, Str$(FolderCount+1) End If End Function Sub ListSubFolders(startfolder$) 'Start$ cannot end in \ 'returns list of folder (full paths) in Folder() - unsorted Dim iPOS As Long Folder(FolderCount) = startfolder$ While Len(Folder(iPOS)) temp$ = Dir$(Build$(Folder(iPOS),"\*.*"), Only %SubDir) 'subfolders only While Len(temp$) Incr FolderCount Folder(FolderCount) = Build$(Folder(iPos),"\",temp$) temp$ = Dir$ (Next) Wend Incr iPOS Wend End Sub
Comment