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

CodePudding user response:

refer to the second floor Chen8013 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



Thank you very much! The first need is 3, 4, 5. The second need is 5 to 7. In a word if there exist the lower straton folder, do not have to, thank you very much!

CodePudding user response:

refer to the second floor Chen8013 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



Dizzy, I'm very sorry upstairs said is wrong, should be both need is 3 to 7, in short if there exist the lower straton folder, do not have to,

CodePudding user response:

 'application example: 
Private Sub Command1_Click ()
Dim aBuff () As String
Dim As Long I

ABuff=EnmuFolders (" E: \ WPS Office ")
'parameters: to enumerate "bottom a folder path,"
'can't start from "drive" or (such as C: \ C:)
'path finally don't bring \ characters
For I=0 & amp; The To UBound (aBuff)
The Debug. Print aBuff (I)
Next
End Sub


 'standard module code: 
Option Explicit

The Public Function EnmuFolders (ByVal TopPath As String) As a String ()
Dim arrBuff () As String
Dim arrTemp () As String
Dim strPath As String
Dim lUSize As Long
Dim lUseNum As Long
Dim lCurPnt As Long
Dim lpValid As Long
Dim i& N&

LpValid=1
LUSize=31
LCurPnt=0
LUseNum=0
ReDim arrBuff (lUSize)
ArrBuff (lUseNum)=TopPath
Do
StrPath=arrBuff (lCurPnt)
N=EnmuSubFolders (strPath, arrTemp)
If (n=0) Then
LpValid=lpValid + 1
ArrBuff (lpValid)=strPath
The Else
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related