41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.IO;
|
|
|
|
namespace bsmd.Tool
|
|
{
|
|
class CleanupFiles
|
|
{
|
|
|
|
/// <summary>
|
|
/// Alle XML Dateien in einem Dateipfad(rootPath) (recursive) löschen, die älter als (staleDays) Tage sind
|
|
/// </summary>
|
|
/// <param name="rootPath"></param>
|
|
/// <param name="staleDays"></param>
|
|
/// <param name="recursive"></param>
|
|
public static void Cleanup(string rootPath, int staleDays, bool recursive)
|
|
{
|
|
if (!Directory.Exists(rootPath)) throw new ArgumentException("directory does not exist");
|
|
foreach (string file in Directory.GetFiles(rootPath, "*.xml"))
|
|
{
|
|
double age = (DateTime.Now - File.GetCreationTime(file)).TotalDays;
|
|
if (age > staleDays)
|
|
{
|
|
Console.WriteLine(file + " - " + age.ToString());
|
|
File.Delete(file);
|
|
}
|
|
}
|
|
|
|
if(recursive)
|
|
{
|
|
foreach (string directory in Directory.GetDirectories(rootPath))
|
|
Cleanup(directory, staleDays, recursive);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|