删除带有最近日期的苍蝇 [英] Flies deletion with recent date

查看:88
本文介绍了删除带有最近日期的苍蝇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要删除文件夹中的1000多个txt文件...

我想删除所有文件,但要删除最近重新创建的5个文本文件..

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..

推荐答案

可能您可以遍历文件,并根据以下每个文件使用以下功能无论您想设置什么时间

File.GetCreationTime(MSDN) [ ^ ]

这样可以节省您创建文件的时间,也可以使用您设置的删除时间与此进行比较.
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.


DirectoryInfo dinfo = new DirectoryInfo(@"D:\ c sharp and asp \ files);
FileInfo []文件= dinfo.GetFiles("*.pdf");
foreach(文件中的FileInfo文件)
{//File.GetCreationTime
如果(file.Exists)
{
System.DateTime dtToday = DateTime.Now;
TimeSpan tdDiff = dtToday-file.LastWriteTime;
int i = 0;
i = tdDiff.Days;

如果(i> 5)
{
file.Delete();

}
其他
{

}
}

我使用这种方式来使用TimeSpan,但是代码删除了所有文件,但最近5天除外..

但我需要最近创建的5个文件并删除较旧的日期文件...
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...


这篇关于删除带有最近日期的苍蝇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆