Click here to Skip to main content
15,883,988 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,
I have written a script in which all the files before 30 days will be Purged or deleted. But I want to know how many files are there and the file names-
1. Count of files deleted
2. Name of the files

I have the below Script already prepared . Can you help me how can I include these two conditions in the script-

foreach (string fileSIPOS2 in Directory.EnumerateFiles(DirNameSIPOS2))
{

FileInfo fiSIPOS2 = new FileInfo(fileSIPOS2);
if (fiSIPOS2.LastWriteTime < DateTime.Now.AddDays(-DaytoDelete))
{
fiSIPOS2.Delete();
}

Thanks in advance
Posted

1 solution

You can use any type of array or generic list object to track files you are removing. Here is a simple way to achieve this.

C#
HashSet<string> deletedFiles = new HashSet<string>();

foreach (string fileSIPOS2 in Directory.EnumerateFiles(DirNameSIPOS2))
{
    FileInfo fiSIPOS2 = new FileInfo(fileSIPOS2);
    
    if (fiSIPOS2.LastWriteTime < DateTime.Now.AddDays(-DaytoDelete))
    {
        deletedFiles.Add(fiSIPOS2.FullName);
        fiSIPOS2.Delete();
    }
}

Console.WriteLine(deletedFiles.Count);

foreach(var entry in deletedFiles)
{
    Console.WriteLine(entry);
}
</string></string>
 
Share this answer
 

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