Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to delete more than 1000 txt files in Folder...

and i want to delete all files but except recently recreated 5 text files..
Posted

Probably you can iterate through your files and use the function below for each file according to whatever time you want to set

File.GetCreationTime (MSDN)[^]

This gets you the time the file was created and you can use the time you have set for deletion to compare with this.
 
Share this answer
 
DirectoryInfo dinfo = new DirectoryInfo(@"D:\c sharp and asp\files");
FileInfo[] Files = dinfo.GetFiles("*.pdf");
foreach (FileInfo file in Files)
{//File.GetCreationTime
if (file.Exists)
{
System.DateTime dtToday = DateTime.Now;
TimeSpan tdDiff=dtToday-file.LastWriteTime;
int i = 0;
i = tdDiff.Days;

if (i >5)
{
file.Delete();

}
else
{

}
}

I am using like this to using TimeSpan but is code delete all files but except recent 5 days files ..

but i need recently created 5 files and delete older date files...
 
Share this answer
 
Comments
Nithin Sundar 29-Aug-12 1:38am    
This is not an answer.

Please update your question. And read my answer. The method I gave is to get the creation date of a file. You can specify a particular time and compare the file's creation time.
Nithin Sundar 29-Aug-12 1:38am    
By the way I meant: Move whatever posted here to your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900