Reading Directory Of Files, DateDiff Function

  • 13 years ago

    I completed a project in VB.NET 2003 and I recently found the need to add a new feature to it.

    My program saves a bunch of text files into a folder called Archive.

    What I would now like to do is to somehow read that folder and compare the CREATION Dates to Now().

    I know the function FileDateTime reads either the Last Accessed or Last Modified date. That is no good because what if I open the text file or if my program does for some reason? then the date changes but the original creation date does not, so I want to read the original creation date.

    I noticed in the help files there was a FileCreated function but that was listed under Internet Development SDK....so that doesn't help because I am not developing a web environment....

    So I have 3 questions .....

    1) How do I get the original creation date of a file? (not the last accessed or last modified dates!!)

    2) How do I read the dates of all files in a folder?

    3) How do I properly use the DateDiff function for the comparison and then tell VB to tell me all the files that are OLDER than 2 years? (message box or label I dont care)

    As an example, every time I try to do something like:

        newdate=DateDiff(DateInterval.Day, FileCreatedDate, Now())

    I get an Overload error even though FileCreatedDate has a valid date in it.

  • 13 years ago

    I would use the following:

    Public Function CheckFileCreateDate(ByVal FolderPath As String, ByVal FileMask as String, ByVal CutoffDate As Date) As Collection(Of String)

          Dim files As ReadOnlyCollection(Of String)

          Dim OldFiles As New Collection(Of String)

          files =

    My.Computer.FileSystem.GetFiles(FolderPath, True, FileMask)

          Dim fileData As FileInfo

          For intLoop As Integer = 0 To files.Count

                fileData =

    My.Computer.FileSystem.GetFileInfo(files.Item(intLoop))

                If DateDiff(DateInterval.Year, fileData.CreationTime, CutoffDate) > 2 Then

                      OldFiles.Add(fileData.Name)

                End If

                Next

          CheckFileCreateDate = OldFiles

    End Function

    You may also want to check the file attributes if system files or hidden files, etc. are an issue.
  • 13 years ago

    I tried this but it throws an error here:

    Dim

    files As ReadOnlyCollection(Of String)

    Error: ReadOnlyCollection is not defined.

    and then here:

    Dim

    OldFiles As New Collection(Of String)

    Error: Microsoft.VisualBasic.Collection has no type parameters and so cannot have type parameters.

     

     

  • 13 years ago

    My appoligies. That is because that syntax is for VB.NET 2005.

     

    Replace

        Dim files As ReadOnlyCollection(Of String)

    with

       Dim files As Collection

    and

       Dim oldfiles As New Collection(Of String)

    with

       Dim oldfiles As New Collection

    What you use to save the list and how you return it is completely up to you. Use what's comfortable and familiar for you. What's important is the use of the FileInformation object and DateDiff function.

     

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Brevity is the soul of wit” - Shakespeare