counting the number of files in a folder

Discussion in 'Visual Basic ( VB )' started by harish13, Jul 27, 2006.

  1. harish13

    harish13 New Member

    Joined:
    Jul 20, 2006
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    i want to count the number of files present in a folder how can i do this
    thank u in advance
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,345
    Likes Received:
    382
    Trophy Points:
    83
  3. ocena

    ocena New Member

    Joined:
    Aug 6, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    if you are using vb 2005 then here it is
    Code:
    Dim SearchCriteria As String = String.Empty  ' Globals
    Dim FileFound As Integer
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            Try
                FileFound = 0 ' reset counter
                MessageBox.Show("Number of files matching : " + GetFileCount(TextBox2.Text).ToString())
            Catch ex As Exception
                MessageBox.Show("Error occured: " + ex.Message)
            End Try
    End Sub
    
    
    Private Function GetFileCount(ByVal path As String) As Integer
            Dim CriteriaPath, files, dirs As String
    
            CriteriaPath = path.Substring(0, path.LastIndexOf("\"))
    
            If SearchCriteria.Length = 0 Then
                SearchCriteria = "*.*"
            Else
                SearchCriteria = path.Substring(path.LastIndexOf("\") + 1)
            End If
    
            For Each files In Directory.GetFiles(CriteriaPath, SearchCriteria)
                FileFound += 1
            Next
    
            For Each dirs In Directory.GetDirectories(CriteriaPath)
                GetFileCount(dirs + "\" + SearchCriteria) ' recursive call
            Next
    
            Return FileFound
    End Function
     
    Last edited by a moderator: Aug 7, 2006

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice