By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,666 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,666 IT Pros & Developers. It's quick & easy.

Delete files in folder based on modified date, but not others.

P: n/a
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.

Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Richard,

I would in your situation do first two things
Set in top of your program
Option Strict On

And start with changing the first line in
Dim First_Date As DateTime = Now.AddDays(-7)

I don't know if it than is working, however now it is definitly wrong.

I hope this helps,

Cor
Nov 21 '05 #2

P: n/a
<ri***********@northwesternmutual.com> schrieb:
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

Do you want to delete the files based on the last write time or based on the
date in their file names?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a
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.
|
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.