Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I find how many files and subfolders are in a folder

Status
Not open for further replies.

Toranaga

Programmer
Nov 14, 2002
11
RO
I'm working on a program in which I need to use a function to show me how many files and subfolders a certain folder contains. You know, just like in Explorer when you click Properties on a folder: it contains X files, X folders . I'm almost sure that it can be done only with API functions, but I couldn't find nothing which I could use.
Can anyone provide some help for me ?

Thanks in advance.
 
This might help
Code:
 Dim fs As Object
 Dim myPath As String
 Dim myFolder
 Dim thing

 fs = CreateObject("scripting.filesystemobject")
 myFolder = fs.getfolder(myPath)
 'This loop searches for sub-folders within the main folder
 For Each thing In myFolder.subfolders   
       'This will do a loop for every subfolder in the selected folder
 Next

'You can also loop for each file in the folder
For Each thing In myFolder.Files
     'This will do a loop for every file in the folder
Next
 
Thank you very much, man, I think this is gonna work. Now I'm trying to "build" a recursive function to count all the
sub-sub-...-sub-folders. I did'n think at all about this solution, because I have never worked with FileSystemObjects.


Bye !
 
I have the code to do this aswell if you want it, I had to design a program to do the exact same thing a few weeks ago
 

I tried (and I finally succeeded) to make this program (thanx to your help). I will write it below, because I wanna know if you made the same thing:

Dim CountFiles As Long
Private Function CountSubFolders(Path As String) As Double
Dim FSO As Object
Screen.MousePointer = vbHourglass
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FolderExists(Path) Then
CountSubFolders = -1
Exit Function
End If
CountSubFolders = CountSubFoldersRec( _
FSO.GetFolder(Path)) - 1
Set FSO = Nothing
Screen.MousePointer = vbNormal
End Function

Function CountSubFoldersRec(Folder As Object) As Double
Dim I As Long, SubFolder As Object, FileObject As Object
CountSubFoldersRec = 1
For Each FileObject In Folder.Files
CountFiles = CountFiles + 1
Next
For Each SubFolder In Folder.SubFolders
CountSubFoldersRec = CountSubFoldersRec + _
CountSubFoldersRec(SubFolder)
Next
End Function
Private Sub Command1_Click()
MsgBox CountSubFolders("C:\") & " folders, " & CountFiles & " files."
End Sub

It works to me pretty fast (PIII, 600MHZ, 256RAM counts Program Files - 16447 Files, 983 Folders in about 4 secs - just as fast as in Explorer).

If your code is different from mine, I'd like to know what you did (I mean I'm not too delighted to use a recursive function to count all the files). I was wondering if there's an API function which counts all the Files straight from the FAT, without being needed a recursive function, which could crash if there are too many iterations.
Do you have any idea how this could be done in C++ ?
 
I just used a recursive function aswell, pretty much the way you have it. I don't have a great knowledge with c++ so I can't help you there.
 
It seems to me that the DirectoryInfo class does everything that you are looking for. You have to import System.IO. As a simple example:

Dim dirInfo As DirectoryInfo
Dim nfiles As Integer = 0
Dim FileList() As FileInfo
Dim subDirs() As DirectoryInfo
Dim subDir as DirectoryInfo
Dim targetDir as String = "D:\"

dirInfo = New DirectoryInfo(targetDir)
subDirs = dirInfo.GetDirectories(targetDir)
FileList = dirInfo.GetFiles("*.*")
nfiles = FileList.GetUpperBound(0) + 1

For each subDir in subDirs
...
Next



End Sub
Dean
---
goddette@san.rr.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top