I have a DB that contains a list of paths to files. I want to build a routine to cleanup the folders, removing files in the directories if there is not a db record for it (for temp ajax file uploads, in cases where the user doesn't complete the form, etc...).
I'm thinking something like this:
var dbFiles = db.allPaths();
var allFiles = Directory.EnumerateFiles(path);
foreach (var f in allFiles) {
if (!dbFiles.Contains(f) {
File.Delete(f);
}
}
Any "Gotchas" waiting for me? The routine will be set to run once a week at first, more often if temp files become a problem. It will be run during a time when there are nearly no users on, so performance - while important - is not paramount.
Thanks in advance!
UPDATE
Wow, lots of great answers. This bit of code is turning into something "share" worthy. ;D My code above was just a simple, quick placeholder bit... but it's transformed into solid code. Thank you!