Get Number of files in a folder(File Count)


This article is written by Pon Saravanan  on 21-Jul-07 Last modified on :06-Jul-08





Sample code to get the file count of a folder using VB6

This function counts only the folder we specified. It will not include the sub folders. To access subfolders we need to use a recursive function.

 

FileSystemObject is a very good object for working with IO operations in classic VB. It has lot of interesting functions. With little bit work we can achieve what we need.

 

To use fileSystemObject to find the count of files

1)      Declare the object

Dim fso As New FileSystemObject

2)      Check whether the folder exists or not

fso.FolderExists(strFolder)

3)      Get the files collection

fso.GetFolder(strFolder).Files

4)      Get the no of elements in the collection that is the count of files
fso.GetFolder(strFolder).Files.Count

5)      Include these in the Error Handlers

 

The following function can be placed in a module or class file. And the same can be used from the other modules like


The same function can be enhanced to find the number of files by extension. That i will explain in the following articles
 

Msgbox GetFileCount("c:\")

 


'******************************************************
'*  Purpose :   Get the  file count
'*
'*  Inputs  :   strFolder(String)           Path to folder
'*
'*  Returns :  File Count(no of files ) for the given folder
'*
'*****************************************************
Public Function GetFileCount(strFolder As String) As Integer
    Dim fso As New FileSystemObject
    
    On Error GoTo ErrTrap
    If Not fso.FolderExists(strFolder) Then
        Err.Raise 1, , "Invalid path  given"
    End If
    
    GetFileCount = fso.GetFolder(strFolder).Files.Count
       
ErrTrap:
    If Err Then 
        Err.Raise Err.Number, , "Error form Functions.GetFileCount " &  Err.Description
    End If
End Function

you need to reference Microsoft Scripting Runtime










Comments
  • GUEST
    its nice 3/12/2010 2:04:10 AM

  • GUEST
    These are VB.net functions, NOT VB6! 5/18/2010 5:46:36 AM

  • pons
    No it is there in VB6 but you need to reference the Microsoft scripting runtime. I was using these functions even before the .Net framework introduced to public.

    5/18/2010 6:05:14 AM

  • GUEST
    5/27/2010 5:08:53 AM

  • GUEST
    10/29/2010 7:30:35 AM

  • GUEST
    12/3/2011 9:19:07 AM

  • GUEST
    12/3/2011 9:19:47 AM

  • GUEST
    uhj 12/25/2011 9:16:24 AM


Comments
   
Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)    



Spam Bot Trap



   



Select Theme
White
Blue
Brown
Gray