Created
December 30, 2014 23:39
-
-
Save ajtowf/e53ed885ffbf6f7f53c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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