Go4Expert (http://www.go4expert.com/)
-   Visual Basic ( VB ) (http://www.go4expert.com/forums/visual-basic/)
-   -   counting the number of files in a folder (http://www.go4expert.com/forums/counting-files-folder-t1075/)

harish13 27Jul2006 17:21

counting the number of files in a folder
i want to count the number of files present in a folder how can i do this
thank u in advance

shabbir 28Jul2006 11:46

Re: counting the number of files in a folder
See my reply to your other thread at http://www.go4expert.com/showthread....42&postcount=2

ocena 7Aug2006 12:03

Re: counting the number of files in a folder
if you are using vb 2005 then here it is
Code: VB

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
            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 = "*.*"
            SearchCriteria = path.Substring(path.LastIndexOf("\") + 1)
        End If

        For Each files In Directory.GetFiles(CriteriaPath, SearchCriteria)
            FileFound += 1

        For Each dirs In Directory.GetDirectories(CriteriaPath)
            GetFileCount(dirs + "\" + SearchCriteria) ' recursive call

        Return FileFound
End Function

All times are GMT +5.5. The time now is 13:06.