Here is a little file remover script I wrote for servers here. It takes either takes 2 arguments or can be run interactively. It looks in a directory specified and removes the files that are x days old. This is nice for servers that fill up with backup files or logs. This goes hand-in-hand with volume size monitoring, of course. Feel free to use/change or comment on the script. Enjoy!
/* * File Remover Program * Author: Mike Dunphy * Seattle Pacific University * Description: This program either takes 2 arguments or can be run interactively. The two arguments are 1. path * where files are to prune and 2. how many days back to delete files */ using System; using System.Collections.Generic; using System.Text; using System.IO; namespace fileRemover { class Program { static void Main(string[] args) { string path = null; string days = null; if (args.Length > 0) { if (args.Length == 2) { path = args[0].ToString(); days = args[1].ToString(); } else { Console.WriteLine("Please enter 2 arguments - Path and days to prune"); System.Environment.Exit(777); } } else { Console.Write("Enter path to prune: "); path = Console.ReadLine(); Console.Write("Enter days back to prune: "); days = Console.ReadLine(); } string formattedDate; formattedDate = DateTime.Now.AddDays(-30).ToString("yyyyMMdd"); if (path != null && days != null) { DirectoryInfo di = new DirectoryInfo(path); FileInfo[] fis = di.GetFiles(); foreach (FileInfo fi in fis) { if (fi.LastWriteTime < DateTime.Now.AddDays(-Int32.Parse(days))) { fi.Delete(); } } } else { Console.WriteLine("Something went seriously wrong. Program takes 2 arguments or is run interactively. Fail."); System.Environment.Exit(777); } } } }
