Skip to content

Instantly share code, notes, and snippets.

@ajtowf
Created December 30, 2014 23:39
Show Gist options
  • Save ajtowf/e53ed885ffbf6f7f53c1 to your computer and use it in GitHub Desktop.
Save ajtowf/e53ed885ffbf6f7f53c1 to your computer and use it in GitHub Desktop.
var officeCacheDir = ConfigurationManager.AppSettings[OfficeCacheFolderKey];
var maxAgeMinutes = int.Parse(ConfigurationManager.AppSettings[LastAccessTimeMinutesBeforeDeleteKey]);
while (true)
{
string[] files = Directory.GetFiles(officeCacheDir).Where(file => Regex.IsMatch(file, @"^.+\.(FSD|FSF)$")).ToArray();
Console.WriteLine("Found {0} files", files.Length);
var deletedCount = 0;
foreach (string file in files)
{
var fi = new FileInfo(file);
if (fi.LastWriteTime < DateTime.Now.AddMinutes(-maxAgeMinutes))
{
try
{
fi.Delete();
Console.WriteLine("Deleted {0}", fi.FullName);
deletedCount++;
}
catch (Exception)
{
Console.WriteLine("Failed to delete {0}, probably being used.", fi.FullName);
}
}
}
Console.WriteLine("Deleted Totally {0} files this iteration", deletedCount);
Console.WriteLine("Sleeping for 1 minute, safe to close application now.");
Thread.Sleep(60 * 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment