delete all files, which older than 5 days

T

Thomas Bauer

Hello,

Call
DeleteFiles bgW_DeleteFilesProcess = new DeleteFiles();
bgW_DeleteFilesProcess.RunAsync( folder, 5, "*.txt", new
RunWorkerCompletedEventHandler( RunWorkerCompleted_DeleteFiles_TXT ) );

I delete all files, which older than 5 days and I use a background
thread.

System.IO.FileInfo[] files = new
System.IO.DirectoryInfo( bw.Path ).GetFiles( bw.Extension,
System.IO.SearchOption.AllDirectories );
Array.ForEach( files, delegate( System.IO.FileInfo file )
{
if ( file.CreationTime.Date <= DateTime.Today.AddDays( -
bw.Days ) )
{
file.Delete();
FilesDeleted++;

}
} );


3 questions:

a)
If I move the files, only for test from folder a to b.
The member file.CreationTime.Date hvae the actual date.
Why that?
I want to use the creation date from the file.
b)
I found this in the net.
###Array.ForEach( files, delegate( System.IO.FileInfo file )#####
Is not claer for me
delegate( System.IO.FileInfo file ) ?

Why delegate?
Can I write another way. A easier way for understanding?
c) Extension
I will delete following files
"*.txt"
"*.log"

I can't say "*.txt, *log".
Is it possible to use both? If yes, how?


Regards Thomas
 
S

sloan

I'll try to help on the file mathing part:


This is old code...in vb.net, but the translation should be simple.




Private m_searchPatterns() As String = New String() {"*.txt", "*.log",
"*.doc"}


Private Sub CleanUpOldFiles(ByVal cacheFolder As String)


'verify existence of image cache folder
If (System.IO.Directory.Exists(cacheFolder )) Then

For i As Int32 = 0 To m_searchPatterns.Length - 1

For Each fileName As String In
System.IO.Directory.GetFiles(cacheFolder , m_searchPatterns(i))
'only look at certain files

Dim ts1 As TimeSpan =
Now.Subtract(System.IO.File.GetLastAccessTime(fileName))
If (ts1.Minutes > m_cacheMinutesLimit) Then
Try
If (System.IO.File.GetAttributes(fileName) And
FileAttributes.ReadOnly) = 0 Then
System.IO.File.Delete(fileName)
End If
Catch ex As Exception
'issue with deleting files, there is a lock on
file
End Try

End If

Next fileName

Next i

End If

End Sub



If I recall correctly, you have to multiple times check for the single
patterns.

Also notice my .ReadOnly check....you want to check for that as well.


I'd wrap it in try/catch, because you don't want to (not) delete most of the
files, because 1 fails.
I ignore a failed file, you can log it or whatever.


Its your business rule, but my checks for the GetLastAccessTime as well.
 
P

Peter Bromberg [C# MVP]

here is a KIVSS ("keep it VERY simple stupid") sample:

public int CleanUpSearches()
{
int searchPurgeDays = 7;
int ctr = 0;
DirectoryInfo di = new DirectoryInfo(Server.MapPath("/Searches"));
FileInfo[] fis = di.GetFiles("*.htm");
foreach (FileInfo fi in fis)
{
if (fi.LastWriteTime < DateTime.Now.AddDays(-searchPurgeDays
))
{
fi.Delete();
// clean from database here or other work

ctr++;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top