Forums Neueste Beiträge
 

delete all files, which older than 5 days

08/01/2008 - 20:04 von Thomas Bauer | Report spam
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
 

Lesen sie die antworten

#1 Herfried K. Wagner [MVP]
08/01/2008 - 21:42 | Warnen spam
Hallo Thomas!

"Thomas Bauer" schrieb:

Hier wird Deutsch gesprochen!

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?



Wie verschiebst Du die Datei? Mit 'File.Move'?

I want to use the creation date from the file.



Was spricht dagegen, die Dateizeit mittels 'File.SetCreationTime[Utc]' auf
den Wert zu setzen, den Du zuvor für die bestehende Datei mit
'File.GetCreationTime[Utc]' ermittelt hast?

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 ) ?



Das ist eine anonyme Methode. In Deinem Beispiel aber recht sinnlos, da wàre
ein 'foreach(FileInfo file in files)' sinnvoller.

c) Extension
I will delete following files
"*.txt"
"*.log"

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



Nein, Du mußt zwei Mal separat 'GetFiles' aufrufen.

M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Ähnliche fragen