Count the Number of specified files in a Directory
Below is a function 'FilesDirectoryCount' that will count the number of files within a directory using The .NET Framework class Directory. The GetFiles method allows us to retrieve all the files with a '.jpg' file extension and we can then use the Length property to return the number of files.
Get Sample Code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim FileCount As Integer = FilesDirectoryCount("C:\Inetpub\wwwroot\website1\images", ".gif")
If FileCount = 0 Then
lblFilesInDirectory.Text = "There are no gif files in the directory."
ElseIf FileCount = 1 Then
lblFilesInDirectory.Text = "There is 1 gif files in the directory."
Else
lblFilesInDirectory.Text = "There are " & FilesDirectoryCount("H:\Inetpub\wwwroot\website1\images", "*.gif") & " gif files in the directory."
End If
End Sub
Function FilesDirectoryCount(ByVal myDirectory As String, ByVal Extension As String) As Integer
Return Directory.GetFiles(myDirectory, "*" & Extension).Length
End Function
200aef1c-d6f0-47b0-8ae7-d1abc0bf368e|0|.0