Delete certain files in folder by 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
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


LVL 2
Richard KreidlSoftware DeveloperAsked:
Who is Participating?
 
amyhxuCommented:
If that still doesn't work, then those are still not the right file name. You can write to console first to see what the exact file names are, and use those in the If statement:

 Sub Main()
        Dim First_Date As String = Date.Today.AddDays(-7)
        Dim Archive_Files() As String = System.IO.Directory.GetFiles("C:\Turnover")

        For Each a As String In Archive_Files
            Console.WriteLine(a)
        Next

        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
0
 
amyhxuCommented:
Do you mean there should always be "Turnover.html" and "Shortcut to Turnover.html" in the "Turnover" directory? If so, just exclude these two files while deleting:

        For Each Found_File As String In Filtered
            If Found_File <> "C:\Turnover\Turnover.html" AndAlso Found_File <> "C:\Turnover\Shortcut to Turnover.html" Then
                System.IO.File.Delete(Found_File)
            End If
        Next
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
It's still deleting the two files I want to keep.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
amyhxuCommented:
I did a little project myself, it is working. Make sure the two files you don't want to delete are exactly "C:\Turnover\Turnover.html" and "C:\Turnover\Shortcut to Turnover.html".
0
 
amyhxuCommented:
Seems there's a shortcut file. You need a little change then:
   If Found_File <> "C:\Turnover\Turnover.html" AndAlso Found_File <> "C:\Turnover\Shortcut to Turnover.html.lnk" Then
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
It works.
Thanks for all your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.