Count File Function

mshahid

Active member
Joined
Aug 23, 2012
Messages
29
Programming Experience
Beginner
I am new in vb. i am working in this program this program run and gives no erro but gives no required result.

Kindly help me



Public
Class Form1
Overloads Function CountFiles(ByVal minSize As Integer, ByVal maxSize As Integer) As Integer
Console.WriteLine(" You have requested the files between " & minSize & "and " & maxSize & " bytes")
Dim files() As String
files = System.IO.Directory.GetFiles("c:\windows")
Dim i, fileCount As Integer
For i = 0 To files.GetUpperBound(0)
Dim FI As New System.IO.FileInfo(files(i))
If FI.Length >= minSize And FI.Length <= maxSize Then
fileCount = fileCount + 1
End If
Next
Return (fileCount)
End Function
Overloads Function CountFiles(ByVal fromDate As Date, ByVal toDate As Date) As Integer
Console.WriteLine(" You have requested the count of files created from" & fromDate & "to" & toDate)
Dim files() As String
files = System.IO.Directory.GetFiles("c:\windows")
Dim i, fileCount As Integer
For i = 0 To files.GetUpperBound(0)
Dim FI As New System.IO.FileInfo(files(i))
If FI.CreationTime.Date >= fromDate And FI.CreationTime.Date <= toDate Then
fileCount = fileCount + 1
End If
Next
Return (fileCount)
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

End Sub
End
Class
 
Hi,

I have had a look at your code and the only thing that is obviously missing is the call to the functions to use them? See the code below which calls the functions from the Button1_Click event.

VB.NET:
Public Class Form1
  Private Function CountFiles(ByVal minSize As Integer, ByVal maxSize As Integer) As Integer
    Console.WriteLine("You have requested the files between " & minSize & " and " & maxSize & " bytes")
    Dim files() As String = System.IO.Directory.GetFiles("c:\windows")
    Dim i, fileCount As Integer
    For i = 0 To files.GetUpperBound(0)
      Dim FI As New System.IO.FileInfo(files(i))
      If FI.Length >= minSize And FI.Length <= maxSize Then
        fileCount += 1
      End If
    Next
    Return (fileCount)
  End Function
 
  Private Function CountFiles(ByVal fromDate As Date, ByVal toDate As Date) As Integer
    Console.WriteLine("You have requested the count of files created from " & fromDate & " to " & toDate)
    Dim files() As String = System.IO.Directory.GetFiles("c:\windows")
    Dim i, fileCount As Integer
    'For i = 0 To files.Count - 1
    For i = 0 To files.GetUpperBound(0)
      Dim FI As New System.IO.FileInfo(files(i))
      If FI.CreationTime.Date >= fromDate And FI.CreationTime.Date <= toDate Then
        fileCount += 1
      End If
    Next
    Return (fileCount)
  End Function
 
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    MsgBox(CountFiles(Now.AddMonths(-12), Now))
    MsgBox(CountFiles(0, 200000))
  End Sub
End Class
I have made a few streamline changes but otherwise all is fine. A point to note is that the "Overloads" Keyword is only necessary if you have previously created a function to "Override" an existing function otherwise you can just ignore this keyword.

Hope this helps,

Cheers,

Ian

PS. If posting code, then please wrap the code in CODE tags to make things easier to read.
 
A point to note is that the "Overloads" Keyword is only necessary if you have previously created a function to "Override" an existing function otherwise you can just ignore this keyword.
Nearly, this is from help for Overloads keyword:
Optional Modifier. You do not have to use the Overloads modifier when you are defining multiple overloaded properties or procedures in the same class. However, if you use Overloads in one of the declarations, you must use it in all of them.
Overriding is used for redefining inherited properties or methods.
 
Regarding using Directory.GetFiles and creating each new FileInfo. It would be simpler in code to use DirectoryInfo.GetFiles which returns each file as a FileInfo, also using For-Each loop. That is all cosmetic changes it would appear, but System.IO was improved with .Net 4 and doing so now will also perform faster. Still it will consume more memory like Directory.GetFiles also does, since it needs to return the array of all files before you can iterate and process it. With Net 4 there's an even better improvement added in DirectoryInfo.EnumerateFiles method that will return one by one file as you iterate it and consume much less memory. Here is an example:
    Private Function CountFiles(ByVal minSize As Integer, ByVal maxSize As Integer) As Integer
        Dim dir As New IO.DirectoryInfo("c:\windows")
        Dim count As Integer
        For Each file In dir.EnumerateFiles
            If file.Length >= minSize AndAlso file.Length <= maxSize Then
                count += 1
            End If
        Next
        Return count
    End Function

Since you're using .Net 4 you can also use the Linq Count extension to do this, and the code gets much more compressed:
    Private Function CountFiles(ByVal minSize As Integer, ByVal maxSize As Integer) As Integer
        Dim dir As New IO.DirectoryInfo("c:\windows")
        Return dir.EnumerateFiles.Count(Function(file) file.Length >= minSize AndAlso file.Length <= maxSize)
    End Function

Notice the use of AndAlso operator, instead of And operator. AndAlso operator is short-circuiting and saves processing time since there is no need to evaluate maxSize limit if you already know minSize limit doesn't match.
 
Back
Top