Answers:
像这样的事情不能做到。
using System.IO;
string[] files = Directory.GetFiles(dirName);
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
if (fi.LastAccessTime < DateTime.Now.AddMonths(-3))
fi.Delete();
}
LastAccessTime
- CreationTime
如果愿意,您应该去寻求财产!
这是1层Lambda:
Directory.GetFiles(dirName)
.Select(f => new FileInfo(f))
.Where(f => f.LastAccessTime < DateTime.Now.AddMonths(-3))
.ToList()
.ForEach(f => f.Delete());
new DirectoryInfo(dir).GetFiles()
比new FileInfo(f)
每个文件都快。
对于那些喜欢过度使用LINQ的人。
(from f in new DirectoryInfo("C:/Temp").GetFiles()
where f.CreationTime < DateTime.Now.Subtract(TimeSpan.FromDays(90))
select f
).ToList()
.ForEach(f => f.Delete());
.ToList()
除了通过匹配文件进行第二次循环外,调用还添加了什么?
List<T>
定义了ForEach
一种可用于将应用于Action<T>
所有元素的方法。不幸的是,没有这样的扩展方法IEnumerable<T>
。
ForEach
的扩展方法IEnumerable<T>
是很久以前,我有时会忘记它没有内置的。
以下是如何获取目录中文件创建时间以及如何查找3个月前(确切地说是90天之前)创建的文件的代码段:
DirectoryInfo source = new DirectoryInfo(sourceDirectoryPath);
// Get info of each file into the directory
foreach (FileInfo fi in source.GetFiles())
{
var creationTime = fi.CreationTime;
if(creationTime < (DateTime.Now- new TimeSpan(90, 0, 0, 0)))
{
fi.Delete();
}
}
ToList()
,DirectoryInfo.GetFiles()
返回FileInfo[]
。
foreach()
循环外部声明一个新变量以保存的值(DateTime.Now- new TimeSpan(90, 0, 0, 0))
。没有理由在循环中重复计算。
System.IO.File类的GetLastAccessTime属性应该有所帮助。
基本上,您可以使用Directory.Getfiles(Path)来获取所有文件的列表。之后,您将遍历列表并按照Keith的建议调用GetLastAccessTim()。
像这样
foreach (FileInfo file in new DirectoryInfo("SomeFolder").GetFiles().Where(p => p.CreationTime < DateTime.Now.AddDays(-90)).ToArray())
File.Delete(file.FullName);
我已经尝试过此代码,并且效果很好,希望这个答案
namespace EraseJunkFiles
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo yourRootDir = new DirectoryInfo(@"C:\yourdirectory\");
foreach (FileInfo file in yourRootDir.GetFiles())
if (file.LastWriteTime < DateTime.Now.AddDays(-90))
file.Delete();
}
}
}
想要在一定时间内删除文件的最典型方法是使用文件的LastWriteTime(上次修改文件的时间):
Directory.GetFiles(dirName)
.Select(f => new FileInfo(f))
.Where(f => f.LastWriteTime < DateTime.Now.AddMonths(-3))
.ToList()
.ForEach(f => f.Delete());
(以上基于Uri的答案,但带有LastWriteTime
。)
每当您听到有人谈论删除某个特定时间范围之前的文件(这是很常见的活动)时,几乎总是在根据文件的LastModifiedTime进行操作。
另外,在非常特殊的情况下,您可以使用以下内容,但请注意以下事项,因为它们带有警告。
CreationTime
.Where(f => f.CreationTime < DateTime.Now.AddMonths(-3))
在当前位置创建文件的时间。但是,请注意,如果文件被复制,将是复制的时间,并且该时间将比文件的CreationTime
时间新LastWriteTime
。
LastAccessTime
.Where(f => f.LastAccessTime < DateTime.Now.AddMonths(-3))
如果要基于上次读取的时间删除文件,可以使用此功能,但是不能保证将其更新,因为可以在NTFS中禁用它。检查fsutil behavior query DisableLastAccess
它是否打开。同样在NTFS下,访问文件后,LastAccessTime可能需要一个小时才能更新。
另外,如果您需要根据创建日期删除文件,则可以使用File.GetCreationTime方法。
system.IO;
List<string> DeletePath = new List<string>();
DirectoryInfo info = new DirectoryInfo(Server.MapPath("~\\TempVideos"));
FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray();
foreach (FileInfo file in files)
{
DateTime CreationTime = file.CreationTime;
double days = (DateTime.Now - CreationTime).TotalDays;
if (days > 7)
{
string delFullPath = file.DirectoryName + "\\" + file.Name;
DeletePath.Add(delFullPath);
}
}
foreach (var f in DeletePath)
{
if (File.Exists(F))
{
File.Delete(F);
}
}
用于网页加载或网络服务或其他任何用途。
我的概念是evrry 7天,我必须不使用DB删除文件夹文件
//Store the number of days after which you want to delete the logs.
int Days = 30;
// Storing the path of the directory where the logs are stored.
String DirPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Substring(6) + "\\Log(s)\\";
//Fetching all the folders.
String[] objSubDirectory = Directory.GetDirectories(DirPath);
//For each folder fetching all the files and matching with date given
foreach (String subdir in objSubDirectory)
{
//Getting the path of the folder
String strpath = Path.GetFullPath(subdir);
//Fetching all the files from the folder.
String[] strFiles = Directory.GetFiles(strpath);
foreach (string files in strFiles)
{
//For each file checking the creation date with the current date.
FileInfo objFile = new FileInfo(files);
if (objFile.CreationTime <= DateTime.Now.AddDays(-Days))
{
//Delete the file.
objFile.Delete();
}
}
//If folder contains no file then delete the folder also.
if (Directory.GetFiles(strpath).Length == 0)
{
DirectoryInfo objSubDir = new DirectoryInfo(subdir);
//Delete the folder.
objSubDir.Delete();
}
}
例如:要在源代码上进入“我的文件夹”项目,我需要打开两个文件夹。我将这种算法设置为每周2天到4小时内
public static void LimpiarArchivosViejos()
{
DayOfWeek today = DateTime.Today.DayOfWeek;
int hora = DateTime.Now.Hour;
if(today == DayOfWeek.Monday || today == DayOfWeek.Tuesday && hora < 12 && hora > 8)
{
CleanPdfOlds();
CleanExcelsOlds();
}
}
private static void CleanPdfOlds(){
string[] files = Directory.GetFiles("../../Users/Maxi/Source/Repos/13-12-2017_config_pdfListados/ApplicaAccWeb/Uploads/Reports");
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
if (fi.CreationTime < DateTime.Now.AddDays(-7))
fi.Delete();
}
}
private static void CleanExcelsOlds()
{
string[] files2 = Directory.GetFiles("../../Users/Maxi/Source/Repos/13-12-2017_config_pdfListados/ApplicaAccWeb/Uploads/Excels");
foreach (string file in files2)
{
FileInfo fi = new FileInfo(file);
if (fi.CreationTime < DateTime.Now.AddDays(-7))
fi.Delete();
}
}
我在作为服务运行的控制台应用程序中使用以下命令,以从App.Settings文件获取目录信息。保留文件的天数也是可配置的,乘以-1即可在DateTime.Now的AddDays()方法中使用。
static void CleanBackupFiles()
{
string gstrUncFolder = ConfigurationManager.AppSettings["DropFolderUNC"] + "";
int iDelAge = Convert.ToInt32(ConfigurationManager.AppSettings["NumDaysToKeepFiles"]) * -1;
string backupdir = string.Concat(@"\", "Backup", @"\");
string[] files = Directory.GetFiles(string.Concat(gstrUncFolder, backupdir));
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
if (fi.CreationTime < DateTime.Now.AddDays(iDelAge))
{
fi.Delete();
}
}
}
SSIS类型的示例..(如果这可以帮助任何人)
public void Main()
{
// TODO: Add your code here
// Author: Allan F 10th May 2019
//first part of process .. put any files of last Qtr (or older) in Archive area
//e.g. if today is 10May2019 then last quarter is 1Jan2019 to 31March2019 .. any files earlier than 31March2019 will be archived
//string SourceFileFolder = "\\\\adlsaasf11\\users$\\aford05\\Downloads\\stage\\";
string SourceFilesFolder = (string)Dts.Variables["SourceFilesFolder"].Value;
string ArchiveFolder = (string)Dts.Variables["ArchiveFolder"].Value;
string FilePattern = (string)Dts.Variables["FilePattern"].Value;
string[] files = Directory.GetFiles(SourceFilesFolder, FilePattern);
//DateTime date = new DateTime(2019, 2, 15);//commented out line .. just for testing the dates ..
DateTime date = DateTime.Now;
int quarterNumber = (date.Month - 1) / 3 + 1;
DateTime firstDayOfQuarter = new DateTime(date.Year, (quarterNumber - 1) * 3 + 1, 1);
DateTime lastDayOfQuarter = firstDayOfQuarter.AddMonths(3).AddDays(-1);
DateTime LastDayOfPriorQuarter = firstDayOfQuarter.AddDays(-1);
int PrevQuarterNumber = (LastDayOfPriorQuarter.Month - 1) / 3 + 1;
DateTime firstDayOfLastQuarter = new DateTime(LastDayOfPriorQuarter.Year, (PrevQuarterNumber - 1) * 3 + 1, 1);
DateTime lastDayOfLastQuarter = firstDayOfLastQuarter.AddMonths(3).AddDays(-1);
//MessageBox.Show("debug pt2: firstDayOfQuarter" + firstDayOfQuarter.ToString("dd/MM/yyyy"));
//MessageBox.Show("debug pt2: firstDayOfLastQuarter" + firstDayOfLastQuarter.ToString("dd/MM/yyyy"));
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
//MessageBox.Show("debug pt2:" + fi.Name + " " + fi.CreationTime.ToString("dd/MM/yyyy HH:mm") + " " + fi.LastAccessTime.ToString("dd/MM/yyyy HH:mm") + " " + fi.LastWriteTime.ToString("dd/MM/yyyy HH:mm"));
if (fi.LastWriteTime < firstDayOfQuarter)
{
try
{
FileInfo fi2 = new FileInfo(ArchiveFolder);
//Ensure that the target does not exist.
//fi2.Delete();
//Copy the file.
fi.CopyTo(ArchiveFolder + fi.Name);
//Console.WriteLine("{0} was copied to {1}.", path, ArchiveFolder);
//Delete the old location file.
fi.Delete();
//Console.WriteLine("{0} was successfully deleted.", ArchiveFolder);
}
catch (Exception e)
{
//do nothing
//Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
//second part of process .. delete any files in Archive area dated earlier than last qtr ..
//e.g. if today is 10May2019 then last quarter is 1Jan2019 to 31March2019 .. any files earlier than 1Jan2019 will be deleted
string[] archivefiles = Directory.GetFiles(ArchiveFolder, FilePattern);
foreach (string archivefile in archivefiles)
{
FileInfo fi = new FileInfo(archivefile);
if (fi.LastWriteTime < firstDayOfLastQuarter )
{
try
{
fi.Delete();
}
catch (Exception e)
{
//do nothing
}
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
因为与解决方案new FileInfo(filePath)
并不容易测试,我建议使用包装像类Directory
,File
而Path
像这样:
public interface IDirectory
{
string[] GetFiles(string path);
}
public sealed class DirectoryWrapper : IDirectory
{
public string[] GetFiles(string path) => Directory.GetFiles(path);
}
public interface IFile
{
void Delete(string path);
DateTime GetLastAccessTime(string path);
}
public sealed class FileWrapper : IFile
{
public void Delete(string path) => File.Delete(path);
public DateTime GetLastAccessTimeUtc(string path) => File.GetLastAccessTimeUtc(path);
}
然后使用类似这样的东西:
public sealed class FooBar
{
public FooBar(IFile file, IDirectory directory)
{
File = file;
Directory = directory;
}
private IFile File { get; }
private IDirectory Directory { get; }
public void DeleteFilesBeforeTimestamp(string path, DateTime timestamp)
{
if(!Directory.Exists(path))
throw new DirectoryNotFoundException($"The path {path} was not found.");
var files = Directory
.GetFiles(path)
.Select(p => new
{
Path = p,
// or File.GetLastWriteTime() or File.GetCreationTime() as needed
LastAccessTimeUtc = File.GetLastAccessTimeUtc(p)
})
.Where(p => p.LastAccessTimeUtc < timestamp);
foreach(var file in files)
{
File.Delete(file.Path);
}
}
}
只需创建一个小的删除功能即可帮助您完成此任务,我已经测试了此代码,并且运行良好。
此功能可删除90天以上的文件以及扩展名为.zip的文件,该文件将从文件夹中删除。
Private Sub DeleteZip()
Dim eachFileInMydirectory As New DirectoryInfo("D:\Test\")
Dim fileName As IO.FileInfo
Try
For Each fileName In eachFileInMydirectory.GetFiles
If fileName.Extension.Equals("*.zip") AndAlso (Now - fileName.CreationTime).Days > 90 Then
fileName.Delete()
End If
Next
Catch ex As Exception
WriteToLogFile("No Files older than 90 days exists be deleted " & ex.Message)
End Try
End Sub