473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
3 10031
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
<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Raj | last post by:
Hi all, Can anyone help me with a script which would delete files or move them to a different folder at some scheduled time..! Please.....!!! Thanks in advance...
0
by: nek | last post by:
Greetings, I am running DB2 WSE V8.1, FP5 on W2K. The problem is not directly related to DB2 but as part of job, I need to manipulate files coming in from all sources. Under a main folder,...
8
by: Adam Clauss | last post by:
I have a folder containing many subfolders (and subfolders and....) all containing various .cs files. Is there any "easy" way to get them all added to the solution. Preferable would be that the...
0
by: richardkreidl | last post by:
Let's say I have the following folder: C:\Archive\Data\Logs\ and within this folder are many files in this format: Cycle Run of Tuesday, February 01, 2005.html Cycle Run of Wednesday, February...
22
by: Cylix | last post by:
I have a 4row x 1col table, I would like to drop all the content of row three. Since Mac IE5.2 does not suppport deleteRow method, I have also try to set the innerHTML=''; but it does not work. ...
1
by: rdemyan | last post by:
I'm using DIR to loop through files in a folder and it's working fine. But can I control the order of the looping. For example, the first file would be the file with the most recent Date Modified...
4
by: viper888 | last post by:
Hi to all, I'm the newly appointed network administrator in our office, and upon scanning all the PCs that are connected to the network, (by the way we're using a windows 2000 server) almost all...
2
by: Thomas Bauer | last post by:
Hello, Call DeleteFiles bgW_DeleteFilesProcess = new DeleteFiles(); bgW_DeleteFilesProcess.RunAsync( folder, 5, "*.txt", new RunWorkerCompletedEventHandler( RunWorkerCompleted_DeleteFiles_TXT )...
3
by: testperl | last post by:
Can anyone help me to make a perl script that could delete 1 week old files based on the date created? I made a folder: (August 16, 2008 to September 1, 2008) 08-16-08 08-17-08 08-18-08...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.