Hi
Try this type of thing:
Dim morethan7daysold As Boolean = (New IO.DirectoryInfo(Path).CreationTime < Now.AddDays(-7))
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
Hi
Try this type of thing:
Dim morethan7daysold As Boolean = (New IO.DirectoryInfo(Path).CreationTime < Now.AddDays(-7))
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