Home > Software engineering > Urgent ask: how to enumerate to the bottom of a folder subfolders (and as long as the bottom)
Urgent ask: how to enumerate to the bottom of a folder subfolders (and as long as the bottom)
Time:09-28
Rookie A folder here there are many subfolders, and then a folder below contains many subfolders... And so many times... Such as c://01 and below a b c a folder, and then a below 1 2 3 subfolders,,, Now I just want to iterate through access to all the bottom a folder (and as long as the bottom, other all don't, a folder path, file name also don't,), contains the full path, It is best to use fast Dir is not? Thank you very much!
CodePudding user response:
Option Explicit
Sub Main () Dim oLeafDir As Collection Dim As Long I
The Set oLeafDir=GetLeafDirectory (" c: \ \ "01)
The Debug. Print "==Leaf==" For I=1 To oLeafDir. Count The Debug. Print oLeafDir (I) Next End Sub
The Function GetLeafDirectory (ByVal Path As String) As a Collection Dim oLeafDir As Collection Dim oStackDir As Collection Dim sPath As String Dim sSubPath As String Dim lSubCount As Long
The Set oLeafDir=New Collection The Set oStackDir=New Collection OStackDir. Add NormalizePath (Path)
While oStackDir. Count & gt; 0 SPath=oStackDir (oStackDir. Count) OStackDir. Remove oStackDir. Count LSubCount=0 'the Debug. Print sPath
SSubPath=Dir (sPath & amp; "*", vbDirectory) While LenB (sSubPath) & lt;> 0 If (sSubPath & lt;> ". ") And (sSubPath & lt;> ".. ") Then If (GetAttr (sPath & amp; SSubPath) And vbDirectory)=vbDirectory Then LSubCount=lSubCount + 1 OStackDir. Add NormalizePath (sPath & amp; SSubPath) End the If End the If
SSubPath=Dir () Wend
If lSubCount=0 Then OLeafDir. Add sPath End the If Wend
The Set GetLeafDirectory=oLeafDir End the Function
The Public Function NormalizePath (ByVal Path As String) As String If $(Path, 1) Right & lt;> "" Then NormalizePath=Path & amp; "" The Else NormalizePath=Path End the If End the Function
CodePudding user response:
You said this is a bit fuzzy, it seems, is likely to return many folder path?
Also, coordinate, but the level of "depth" is not the same, is not need to return? Take your "C: \ 01" in the ZhuTie example:
C: \ 01 C: \ \ a 01 C: \ \ \ 1 a 01 C: \ \ a \ 2 01 C: \ \ a \ 3 01 C: \ \ b 01 C: \ \ C 01
In this case, it is only three, three, four, five this is returned or three to seven this five? From 6, 7, after all, these two paths, it is an indication of their "bottom",
Another more complicated situation:
C: \ 01 C: \ \ a 01 C: \ \ \ 1 a 01 C: \ \ a \ 2 01 C: \ \ a \ \ 3 x/y 01 C: \ \ b 01 C: \ \ C \ 01 w
Then three to seven is returned? Or 5, and 7? Or is only five?
So, from your ambiguous problem description, simply can't figure out what you want as a result, the