Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

rkckjk

IS-IT--Management
Apr 27, 2001
34
US
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.


 
If File.GetLastWriteTime(Archive_Files(x)) < First_Date Then

as I see the files .html are in a certain format -> STARTING with aDIGIT <-

So: ...
Code:
If File.GetLastWriteTime(Archive_Files(x)) < First_Date  [b]And x.Chars(0).IsDigit(x.Substring(0) = True[/b] then
  Filtered.Add(Archive_Files(x))
End If


The "x.Chars(0).IsDigit(x.Substring(0)" returns TRUE if the first letter of "x" is a digit.



-bclt
 
I get the following two errors on this statement:

If File.GetLastWriteTime(Archive_Files(x)) < First_Date And x.Chars(0).IsDigit(x.Substring(0)) = True Then
Filtered.Add(Archive_Files(x))
End If

Chars is not a memeber of integer
Substring is not a memeber of integer
 
Oupsss ...

If File.GetLastWriteTime(Archive_Files(x)) < First_Date And Archive_Files(x).Chars(0).IsDigit(Archive_Files(x).Substring(0)) = True Then
Filtered.Add(Archive_Files(x))
End If



-bclt

 
The statement doesn't generate any errors but it doesn't delete any files at all.
 
If the files you wish to keep irrespective of their date are

Turnover.html
Shortcut to Turnover.html

then why not test for the name of the file before deleting it?

Test the name of the file, if it is not one of the files to keep then
If File.GetLastWriteTime(Archive_Files(x)) < First_Date Then
Filtered.Add(Archive_Files(x))
End If
End If



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top