Directory creaton Date in VB.Net

~OSD~ 2,201 Reputation points
2022-12-11T19:44:34.467+00:00

Hi,

I have simple scenario where I am copying a text file to a directory as:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
        If IO.Directory.Exists("D:\Lab") Then  
            IO.File.Copy("C:\update.txt", "D:\Lab\Update.txt")  
        Else  
            Me.Close()  
        End If  
    End Sub  

What to check the directory creation date (not the last access date, but only the original creation date) and if directory is 7 days old, copy the update.txt file otherwise, no need to copy and exit the application.

Question: How I can modify the code to determine and make a check if directory was originally 7 days old or not.
Any suggestion will be appreciated here.

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. LesHay 7,141 Reputation points
    2022-12-11T19:55:46.573+00:00

    Hi
    Try this type of thing:

        Dim morethan7daysold As Boolean = (New IO.DirectoryInfo(Path).CreationTime < Now.AddDays(-7))  
      
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. RLWA32 49,536 Reputation points
    2022-12-11T19:59:20.437+00:00
    0 comments No comments

  2. LesHay 7,141 Reputation points
    2022-12-12T14:28:42.913+00:00

    Hi
    To avoid some confusion, here is another example test scenario, This example should be better as it only changes the file creation times to a fixed amount before 'Now' - the previous example would have changed the creation time each time run (which actually wasn't planned for in the example. This new version will not do the same.

        ' test directory with several  
        ' test files Test1....Test5  
        Dim p As String = "C:\Users\lesha\Desktop\TestFiles"  
      
        ' set some creation dates before and  
        ' after 7 days old  
        Dim n As Integer = 5  
        For Each f As String In IO.Directory.GetFiles(p)  
          Dim fi As New IO.FileInfo(f)  
          IO.File.SetCreationTime(f, Now.AddDays(-n))  
          n += 1  
        Next  
      
        For Each f As String In IO.Directory.GetFiles(p)  
          Dim old As Boolean = IO.File.GetCreationTime(f) < Now.AddDays(-7)  
          If old Then  
            MessageBox.Show(IO.Path.GetFileNameWithoutExtension(f) & vbCrLf & "is 7 or more days old.")  
          Else  
            MessageBox.Show(IO.Path.GetFileNameWithoutExtension(f) & vbCrLf & "is less than or equal to 7 days old")  
          End If  
        Next  
      
        Stop  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.