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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete first 15 files in the folder from the current Date 1

Status
Not open for further replies.

jessiejane

Programmer
Oct 29, 2009
32
0
0
US
I have an archive folder with the files in it.
The files are in the format of NJDAT120909.txt format. i.e. NJDAT+Date.txt

The archive folder has many files. The file is not placed during weekends. Only weekdays the file is placed in the archive folder

For example:
---Nov 1st, sunday the file is not there
NJDAT110209.txt
NJDAT110309.txt
NJDAT110409.txt
NJDAT110509.txt
NJDAT110609.txt ----Nov 7,8 files are not there, sat, sun
NJDAT110909.txt
NJDAt111009.txt
...
...
NJDAT120909.txt

How would I delete the first 15 files from the current date.
After deleting, the folder must have remaining files in it.


 
I to clarify we are only at the 9th of December (give or take where you live time zone wise) so with one file a day as in your example I'm not sure what you mean by 15 files in the current date. So you really need to clarify what you mean. Current date being the date of the oldest file? Maybe you meant of the preceding month (November from your example)?

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
The archive folder has many files.

I need only the last 15 days files(for example: Dec 15-Jan1) in the archive folder and need to delete the remaining files.

The process (The delete program) runs once a week, so it check the last 15 days files and deletes the files which are older than 15 days.

Any hints on how to do this? Any help is appreciated.
 
Both of these will delete files that are 15 days and more older. If this isn't what you need it should be enough to get you started.

This method is only good if you can trust the nameing convention of your files.
Code:
    Private Sub Delete15DayNameFiles(ByVal FileLocation As String)
        Dim files As String() = IO.Directory.GetFiles(FileLocation)

        For Each CurrentFile As String In files
            Dim FileName As String = IO.Path.GetFileNameWithoutExtension(CurrentFile)
            Dim FileNameAsDate As Date
            Dim DidConvert As Boolean = Date.TryParseExact(FileName.Substring(5), "MMddyy", _
                                            New Globalization.CultureInfo("en-US"), Globalization.DateTimeStyles.None, FileNameAsDate)

            If DidConvert Then
                If FileNameAsDate < Now.AddDays(-15) Then
                    IO.File.Delete(CurrentFile)
                End If
            End If
        Next
    End Sub

This method is better as long as nothing writes to the same file after it is created.
Code:
    Private Sub Delete15DayFiles(ByVal FileLocation As String)
        Dim files As String() = IO.Directory.GetFiles(FileLocation)

        For Each CurrentFile As String In files
            Dim FileInfoDate As Date = IO.File.GetLastWriteTime(CurrentFile)

            If FileInfoDate < Now.AddDays(-15) Then
                IO.File.Delete(CurrentFile)
            End If
        Next
    End Sub

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Hey the second method worked well but I still have one more issue.

If the date on the file is less than 15 days then it deletes the file. But how would I take care of weekends.
The weekend should be removed from those 15 days. How would I do that?


If FileInfoDate <b> Now.AddDays(-15) </b> Then
IO.File.Delete(CurrentFile)
End If
 
I'm not really sure as I've never had to ignore weekends. We work with full weeks. Just off the top of my head you might see if you can use a calendar control in some way to do it.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 

In a 15-day time span, there will always be at least 2 weekends. If this is being run on a Friday you will need to account for 3 weekends to get 15 week days.

Dim DaysBack As Integer = 0

If Date.Now.DayOfWeek = DayOfWeek.Friday Then
DaysBack = -21 'add 6 days to account for 3 weekends
Else
DaysBack = -19 'add 4 days to account for 2 weekends
EndIf

If FileInfoDate < Now.AddDays(DaysBack) Then
IO.File.Delete(CurrentFile)
End If

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top