• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

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


0
Richard Kreidl
Asked:
Richard Kreidl
  • 4
  • 2
1 Solution
 
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
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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
 
Richard KreidlSoftware DeveloperAuthor Commented:
It works.
Thanks for all your help.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now