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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number of Files/Directories in a Directory

Status
Not open for further replies.

tsosiel

Programmer
Aug 10, 2001
42
US
I'm looking for a function that will return the number of files and/or directories in a specified path. I'm sure there is windows API that I don't know about.

Thank you in advance for any suggestions.

- Lorentz
 
'I'm not sure this will count directries, but it should give you a file count. Set .SearchSubFolders = True to count into subdirectories.
'********

Public Function FileCount(MyPathname As String) As Integer
With Application.FileSearch
.NewSearch
.LookIn = MyPathname
.SearchSubFolders = False
'.FileName = "FileCriteriaHere"
'.MatchTextExactly = True
.FileType = 1
.Execute
FileCount = .FoundFiles.Count
End With

End Function

meh.
 
This will count files and folders, you can convert to a function to count one or the other, or make into two functions and pass the file path.

Sub gFileCount()
Dim objFSO As Object
Dim strPath As String

strPath = "C:\winnt"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(strPath) Then
MsgBox objFSO.GetFolder(strPath).Files.Count & " Files Found"
MsgBox objFSO.GetFolder(strPath).SubFolders.Count & " Folders Found"
Else
MsgBox "Folder not found !"
End If

Set objFSO = Nothing

End Sub

There are two ways to write error-free programs; only the third one works.
 
BTW you will have to reference Microsoft Scripting Runtime

There are two ways to write error-free programs; only the third one works.
 
Check the Help file for information on the Dir() function. There is an example there of searching a path for directories. Just change the example to increment a counter for all files, not just directories, and you should have it.
It may look like this:
Code:
Dim MyFile, MyPath, MyName
Dim DirCount, FileCount as Long

MyPath = "c:\"    ' Set the path.
MyName = Dir(MyPath, vbDirectory)    ' Retrieve the first entry.
Do While MyName <> &quot;&quot;    ' Start the loop.
    ' Ignore the current directory and the encompassing directory.
    If MyName <> &quot;.&quot; And MyName <> &quot;..&quot; Then
        ' Use bitwise comparison to check if MyName is a directory.
        If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
            DirCount = DirCount + 1  ' Count if a directory
        Else 'Just a file
            FileCount = FileCount + 1 'Count in a file
        End If  
    End If
    MyName = Dir    ' Get next entry.
Loop

Msgbox &quot;There are &quot; & DirCount & &quot; folders and &quot; & FileCount & &quot; files in &quot; & MyPath & &quot;.&quot;
 
Thank you for all the help! I actually looked at the Dir() function and got something similar to AccessAce's example. I like the use of the Scripting object - it worked too...

Thanks again to you all.

- Lorentz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top