Richard,
As Cor suggests, use Option Strict On.
Date.Today.AddDays (as does Now.AddDays) returns a DateTime, not a String.
Instead of:
| Dim First_Date As String = Date.Today.AddDays(-7)
Try:
Dim First_Date As DateTime = DateTime.Today.AddDays(-7)
| The module does what it's supposed to do, but it also deletes these two
| files:
| Turnover.html
| Shortcut to Turnover.html
How does that joke go: This guy walks into the doctor & says, it hurts when
I bend my arm. The doctor says, don't bend your arm.
If you don't want those two files deleted in the folder, you either need to
make sure there Last Write Time on them is "today" or you simply need to
exclude them from your filtered array list...
Something like:
Sub Main()
Dim First_Date As DateTime = DateTime.Today.AddDays(-7)
Dim Filtered As New ArrayList
For Each Turnover_File As String In
Directory.GetFiles("C:\Turnover")
If Turnover_File = "Turnover.html" Then
' exclude file
ElseIf Turnover_File = "Shortcut to Turnover.html" Then
' exclude file
ElseIf File.GetLastWriteTime(Turnover_File) < First_Date Then
Filtered.Add(Turnover_File)
End If
Next
For Each Found_File As String In Filtered
System.IO.File.Delete(Found_File)
Next
End Sub
Alternatively I would consider having an "excluded" list of files that
should not be deleted, or possibly a regular expression that matches on file
name patterns for books to delete...
Note I changed the first For loop to a For Each to avoid an "Oddball
Solution", an "Oddball Solution" is when you have two or more similar
constructs (iterating a list) & you do it two or more different ways (a For
index & For Each). "Oddball Solution" is a "code smell" that is identified
in "Refactoring to Patterns"
http://www.industriallogic.com/xp/refactoring/
Hope this helps
Jay
<ri***********@northwesternmutual.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
|I have the following module that I delete old files based on how old
| they are:
|
| Sub Main()
| Dim First_Date As String = Date.Today.AddDays(-7)
| Dim Archive_Files() As String =
| System.IO.Directory.GetFiles("C:\Turnover")
| Dim Filtered As New ArrayList
| For x As Integer = 0 To Archive_Files.Length - 1
| If File.GetLastWriteTime(Archive_Files(x)) < First_Date
| Then
| Filtered.Add(Archive_Files(x))
| End If
| Next
| For Each Found_File As String In Filtered
| System.IO.File.Delete(Found_File)
| Next
| End Sub
|
|
| The files in the 'Turnover' directory are:
|
| 4_11_2005.html
| 4_12_2005.html
| 4_13_2005.html
| 4_14_2005.html
| 4_15_2005.html
| 4_16_2005.html
| 4_17_2005.html
| 4_18_2005.html
| 4_19_2005.html
| 4_20_2005.html
| 4_21_2005.html
| 4_22_2005.html
| 4_23_2005.html
| 4_24_2005.html
| Turnover.html
| Shortcut to Turnover.html
|
|
| The module does what it's supposed to do, but it also deletes these two
| files:
| Turnover.html
| Shortcut to Turnover.html
|
| Which I don't want deleted.
|