Enumerate a Folder and it's Sub Folders 
Author Message
 Enumerate a Folder and it's Sub Folders

I've been hunting and searching to find out if / or how to Enumerate a
Folder and all it's Sub Folders.

In my particular case I have a folder named, "Projects". It has many
Sub Folders under it as well as Sub Folders under those Sub Folders.

How can I extract these folder names, including the start folder i.e
"Projects", count and display all these folders?

Cheers
Roders



Sun, 13 Nov 2005 04:04:44 GMT  
 Enumerate a Folder and it's Sub Folders
The following is a simplification of code that I use for most general
purpose recursive file analysis routines. The example, as shown, gets a list
of all the folders in a specified drive or directory into a List Box and
tells you how many folders and how many files it contains.

Mike

Option Explicit
Option Compare Binary
Private Declare Function FindFirstFile Lib "kernel32" _
 Alias "FindFirstFileA" (ByVal lpFileName As String, _
 lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" _
 Alias "FindNextFileA" (ByVal hFindFile As Long, _
 lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" _
 (ByVal hFindFile As Long) As Long
Private Type FILETIME
   dwLowDateTime As Long
   dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
    lngFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    lngFileSizeHigh As Long
    lngFileSizeLow As Long
    lngReserved0 As Long
    lngReserved1 As Long
    FilenameName As String * 260
    strAlternate As String * 14
End Type
Private TotalFolders As Long
Private TotalFiles As Long
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10

Private Sub CountFiles(Folder As String)
Dim mydata As WIN32_FIND_DATA
Dim hFind As Long, Filename As String, Search As String
If Right$(Folder, 1) <> "\" Then Folder = Folder & "\"
Search = Folder & "*.*"
hFind = FindFirstFile(Search, mydata)
If hFind > 0 Then
Do
Filename = Left$(mydata.FilenameName, _
    InStr(mydata.FilenameName, Chr$(0)) - 1)
If (mydata.lngFileAttributes And _
    FILE_ATTRIBUTE_DIRECTORY) = _
    FILE_ATTRIBUTE_DIRECTORY Then
    If Filename = "." Or Filename = ".." Then
      '
      Else
      ' It is a folder
    TotalFolders = TotalFolders + 1
    List1.AddItem Format(TotalFolders) & "  " & Folder & Filename
    CountFiles Folder & Filename ' recursive call to check sub folders
    End If
Else
    ' It is a file
    TotalFiles = TotalFiles + 1
End If
Loop While CBool(FindNextFile(hFind, mydata))
Call FindClose(hFind)
End If
End Sub

Private Sub Command1_Click()
Caption = "Please wait . . ."
CountFiles "c:\"
Caption = "Number of Folders = " & Format(TotalFolders) _
  & " : Number of files = " & Format(TotalFiles)
End Sub


Quote:
> I've been hunting and searching to find out if / or how to Enumerate a
> Folder and all it's Sub Folders.

> In my particular case I have a folder named, "Projects". It has many
> Sub Folders under it as well as Sub Folders under those Sub Folders.

> How can I extract these folder names, including the start folder i.e
> "Projects", count and display all these folders?

> Cheers
> Roders



Sun, 13 Nov 2005 04:47:11 GMT  
 Enumerate a Folder and it's Sub Folders

Quote:
> I've been hunting and searching to find out if / or how to Enumerate a
> Folder and all it's Sub Folders.

> In my particular case I have a folder named, "Projects". It has many
> Sub Folders under it as well as Sub Folders under those Sub Folders.

> How can I extract these folder names, including the start folder i.e
> "Projects", count and display all these folders?

> Cheers
> Roders

Have a look at Randy Birchs' site www.mvps.org/vbnet He has all the code to
do what you want ie recursively search a folder


Sun, 13 Nov 2005 04:28:03 GMT  
 Enumerate a Folder and it's Sub Folders
On Tue, 27 May 2003 21:47:11 +0100, "Mike Williams"

Mike you are a star!

I had managed to count all files by using Randy's examples but failed
to figure out to count the folders. Your example code does exactly
what I need it for.

Thanks again Mike.

Quote:
>The following is a simplification of code that I use for most general
>purpose recursive file analysis routines. The example, as shown, gets a list
>of all the folders in a specified drive or directory into a List Box and
>tells you how many folders and how many files it contains.

>Mike

>Option Explicit
>Option Compare Binary
>Private Declare Function FindFirstFile Lib "kernel32" _
> Alias "FindFirstFileA" (ByVal lpFileName As String, _
> lpFindFileData As WIN32_FIND_DATA) As Long
>Private Declare Function FindNextFile Lib "kernel32" _
> Alias "FindNextFileA" (ByVal hFindFile As Long, _
> lpFindFileData As WIN32_FIND_DATA) As Long
>Private Declare Function FindClose Lib "kernel32" _
> (ByVal hFindFile As Long) As Long
>Private Type FILETIME
>   dwLowDateTime As Long
>   dwHighDateTime As Long
>End Type
>Private Type WIN32_FIND_DATA
>    lngFileAttributes As Long
>    ftCreationTime As FILETIME
>    ftLastAccessTime As FILETIME
>    ftLastWriteTime As FILETIME
>    lngFileSizeHigh As Long
>    lngFileSizeLow As Long
>    lngReserved0 As Long
>    lngReserved1 As Long
>    FilenameName As String * 260
>    strAlternate As String * 14
>End Type
>Private TotalFolders As Long
>Private TotalFiles As Long
>Private Const FILE_ATTRIBUTE_DIRECTORY = &H10

>Private Sub CountFiles(Folder As String)
>Dim mydata As WIN32_FIND_DATA
>Dim hFind As Long, Filename As String, Search As String
>If Right$(Folder, 1) <> "\" Then Folder = Folder & "\"
>Search = Folder & "*.*"
>hFind = FindFirstFile(Search, mydata)
>If hFind > 0 Then
>Do
>Filename = Left$(mydata.FilenameName, _
>    InStr(mydata.FilenameName, Chr$(0)) - 1)
>If (mydata.lngFileAttributes And _
>    FILE_ATTRIBUTE_DIRECTORY) = _
>    FILE_ATTRIBUTE_DIRECTORY Then
>    If Filename = "." Or Filename = ".." Then
>      '
>      Else
>      ' It is a folder
>    TotalFolders = TotalFolders + 1
>    List1.AddItem Format(TotalFolders) & "  " & Folder & Filename
>    CountFiles Folder & Filename ' recursive call to check sub folders
>    End If
>Else
>    ' It is a file
>    TotalFiles = TotalFiles + 1
>End If
>Loop While CBool(FindNextFile(hFind, mydata))
>Call FindClose(hFind)
>End If
>End Sub

>Private Sub Command1_Click()
>Caption = "Please wait . . ."
>CountFiles "c:\"
>Caption = "Number of Folders = " & Format(TotalFolders) _
>  & " : Number of files = " & Format(TotalFiles)
>End Sub



>> I've been hunting and searching to find out if / or how to Enumerate a
>> Folder and all it's Sub Folders.

>> In my particular case I have a folder named, "Projects". It has many
>> Sub Folders under it as well as Sub Folders under those Sub Folders.

>> How can I extract these folder names, including the start folder i.e
>> "Projects", count and display all these folders?

>> Cheers
>> Roders

Cheers
Roders


Mon, 14 Nov 2005 01:29:28 GMT  
 Enumerate a Folder and it's Sub Folders
On Wed, 28 May 2003 06:28:03 +1000, "Harry Strybos"

Hi Harry

Thanks for the reply. I was using Randy's example code but could not
figure how to include folders. My good mate, Mike, came up with a
solution which precisely fell in to my requirements.

Quote:


>> I've been hunting and searching to find out if / or how to Enumerate a
>> Folder and all it's Sub Folders.

>> In my particular case I have a folder named, "Projects". It has many
>> Sub Folders under it as well as Sub Folders under those Sub Folders.

>> How can I extract these folder names, including the start folder i.e
>> "Projects", count and display all these folders?

>> Cheers
>> Roders

>Have a look at Randy Birchs' site www.mvps.org/vbnet He has all the code to
>do what you want ie recursively search a folder

Cheers
Roders


Mon, 14 Nov 2005 01:34:27 GMT  
 Enumerate a Folder and it's Sub Folders

Quote:

> Mike you are a star!

You're only saying that 'cause it's true ;-)

Mike



Mon, 14 Nov 2005 01:35:08 GMT  
 Enumerate a Folder and it's Sub Folders
On Wed, 28 May 2003 18:35:08 +0100, "Mike Williams"

Quote:



>> Mike you are a star!

>You're only saying that 'cause it's true ;-)

Just when we thought M. Monroe was really dead.. ;-p

--

Regards, Frank



Mon, 14 Nov 2005 07:11:03 GMT  
 Enumerate a Folder and it's Sub Folders


 In Line

Quote:
>On Wed, 28 May 2003 18:35:08 +0100, "Mike Williams"



>>> Mike you are a star!
>>You're only saying that 'cause it's true ;-)
>Just when we thought M. Monroe was really dead.. ;-p

Take heart Frank. There were a couple of flaws in Mikes example which
were easy enough <for me> to sort out, so we will remove one star from
Mike's laurels :-)

Cheers
Roders



Tue, 15 Nov 2005 02:14:11 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Build XML of folders, sub folders and files from specified folder

2. To get the names of folders and sub folders

3. Find Sub Folder Name within a Folder

4. Deleting certian Files in Folders and there sub folders

5. Copy the files and sub folders to another folder

6. Folders that aren't folders.

7. Using folder object when the user doesn't have access to the folder

8. folder in a folder in a folder...

9. Determining if a folder is a local pst folder or a imap (server folder)

10. How to delete folders/files within a folder but not the folder itself

11. Creating (sub-sub-)folders

12. How do I display a non default folder that is a sub folder of Personal Folders?

 

 
Powered by phpBB® Forum Software