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

File Count

Status
Not open for further replies.

adimulam

Programmer
Feb 13, 2002
25
US
I wanted to get a file count of a specific type in a given folder. Is there any function in VB so that I can get the file count if I pass type of the file and path as arguments?. I would appreciate your help.

Thanks,
Srini
 
Here's one way...

Code:
'add reference to Microsoft Scripting Runtime
Private Sub main()

    MsgBox CountFiles("c:\")
    MsgBox CountFiles("c:\", "txt")

End Sub

Private Function CountFiles(ByVal folderPath As String, Optional ByVal fileType As String = "*")

    Dim oFSO As New FileSystemObject
    Dim oFolder As Folder
    Dim oFiles As Files
    Dim oFile As File
    Dim count As Long
    Set oFolder = oFSO.GetFolder("c:\")
    Set oFiles = oFolder.Files
    
    fileType = LCase(Trim(fileType))
    
    If fileType = "*" Then
        CountFiles = oFiles.count
    Else
        For Each oFile In oFiles
            If LCase(oFSO.GetExtensionName(oFile.Name)) = fileType Then count = count + 1
        Next oFile
        CountFiles = count
   End If

End Function

HtH,

Rob

-Focus on the solution to the problem, not the obstacles in the way.-
 
Or use the simple Dir command
Code:
 Dim strFiles As String
 Dim lngCount As Long
   strFiles = Dir("c:\*.doc")
   Do Until Len(strFiles) = 0
      strFiles = Dir
      lngCount = lngCount + 1
   Loop
   MsgBox lngCount & " Files"

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Essex Steam UK for steam enthusiasts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top