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(file Name))
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.
"Thomas Bauer" <ma******@arcor.dewrote in message
news:50**********************************@v4g2000h sf.googlegroups.com...
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