Check file creation time using VB.Net

~OSD~ 2,131 Reputation points
2022-10-19T20:19:43.737+00:00

Hi,

I have a text file C:\Time.txt and here I have written a time stamp, like following:

15/3/2022 3:30:02 PM

Is it possible to check with VB if this file is

  • 10 days old (after comparing the time in time stamp of file). and if YES, delete it otherwise don't delete it?
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,788 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,636 questions
Visual Studio Setup
Visual Studio Setup
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Setup: The procedures involved in preparing a software program or application to operate within a computer or mobile device.
990 questions
{count} votes

2 answers

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2022-10-19T20:51:41.85+00:00

    Hi
    One way ........................

        ' test file path  
        Dim path As String = "C:\Users\lesha\Desktop\freddy.txt"  
      
        ' just for this example so  
        ' that various Dates can be tried  
        IO.File.SetCreationTime(path, Now.AddDays(-11))  
      
        Dim d As DateTime = IO.File.GetCreationTime(path) '.CreationTime  
      
        If d.AddDays(10) <= Now Then  
          ' here is where the file would  
          ' be deleted  
          '      IO.File.Delete(path)  
          Stop  
        End If  
    
    1 person found this answer helpful.

  2. LesHay 7,126 Reputation points
    2022-10-20T13:14:58.587+00:00

    Hi
    Here is a variation using exactly the same code but wrapped in a loop to cycle through dates varying from now to now-11 days. The results are shown in the ListBox1.

    Everything is as expected in this test.

    The only thing I can think of is that you tried to use the example code I posted on the same file that you later tried with the amended code - the point being, the Creation date of the file may have been changed by the first trial and so was seen to be an 'old' file on later trials. When developing and testing, you should NEVER use actual data/files/things to test with, only use items specifically made for testing with (eg a COPY of a file). Also, you should never just copy/paste code from the forum and test on valid data without verifying that it is working correctly.

    To try this code out on a TEST FILE, put a ListBox1 onto Form1 and run this code.

    REMEMBER: there is code in this example to actually chane a file creation date so that the example works. You must not use the line containing 'SetCreationTime' on your own files.

    252484-111.png

    Option Strict On  
    Option Explicit On  
    Public Class Form1  
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
      
        ' test file path  
        Dim path As String = "C:\Users\lesha\Desktop\freddy.txt"  
      
      
        For i As Integer = 1 To 11  
          IO.File.SetCreationTime(path, Now.AddDays(-i))  
      
          Dim d As DateTime = IO.File.GetCreationTime(path)  
      
          If d.AddDays(10) <= Now Then  
            ListBox1.Items.Add("Create Date " & d.ToString & "   Deleted")  
          Else  
            ListBox1.Items.Add("Create Date " & d.ToString & "   NOT Deleted")  
          End If  
        Next  
      End Sub  
    End Class