By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,435 Members | 2,033 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,435 IT Pros & Developers. It's quick & easy.

delete all files, which older than 5 days

P: n/a
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
Jan 8 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a

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

Jan 8 '08 #2

P: n/a
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++;
}
}

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
"Thomas Bauer" wrote:
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
Jan 8 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.