From 3682a807905eb5a4612f82923c1e4572816532a0 Mon Sep 17 00:00:00 2001 From: Brychan Dempsey Date: Wed, 6 Oct 2021 15:17:46 +1300 Subject: [PATCH] Added solution files --- IP_Camera_Cleanup.sln | 31 +++ IP_Camera_Cleanup/App.config | 6 + IP_Camera_Cleanup/IP_Camera_Cleanup.csproj | 77 ++++++ IP_Camera_Cleanup/Program.cs | 254 +++++++++++++++++++ IP_Camera_Cleanup/Properties/AssemblyInfo.cs | 36 +++ 5 files changed, 404 insertions(+) create mode 100644 IP_Camera_Cleanup.sln create mode 100644 IP_Camera_Cleanup/App.config create mode 100644 IP_Camera_Cleanup/IP_Camera_Cleanup.csproj create mode 100644 IP_Camera_Cleanup/Program.cs create mode 100644 IP_Camera_Cleanup/Properties/AssemblyInfo.cs diff --git a/IP_Camera_Cleanup.sln b/IP_Camera_Cleanup.sln new file mode 100644 index 0000000..93caa4b --- /dev/null +++ b/IP_Camera_Cleanup.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.705 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IP_Camera_Cleanup", "IP_Camera_Cleanup\IP_Camera_Cleanup.csproj", "{CCA56B64-383E-4707-87F5-39A4DD7BFD01}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CCA56B64-383E-4707-87F5-39A4DD7BFD01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CCA56B64-383E-4707-87F5-39A4DD7BFD01}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CCA56B64-383E-4707-87F5-39A4DD7BFD01}.Debug|x64.ActiveCfg = Debug|x64 + {CCA56B64-383E-4707-87F5-39A4DD7BFD01}.Debug|x64.Build.0 = Debug|x64 + {CCA56B64-383E-4707-87F5-39A4DD7BFD01}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CCA56B64-383E-4707-87F5-39A4DD7BFD01}.Release|Any CPU.Build.0 = Release|Any CPU + {CCA56B64-383E-4707-87F5-39A4DD7BFD01}.Release|x64.ActiveCfg = Release|x64 + {CCA56B64-383E-4707-87F5-39A4DD7BFD01}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {69725A90-A937-4B27-8719-636390ED8DE8} + EndGlobalSection +EndGlobal diff --git a/IP_Camera_Cleanup/App.config b/IP_Camera_Cleanup/App.config new file mode 100644 index 0000000..d1428ad --- /dev/null +++ b/IP_Camera_Cleanup/App.config @@ -0,0 +1,6 @@ + + + + + + diff --git a/IP_Camera_Cleanup/IP_Camera_Cleanup.csproj b/IP_Camera_Cleanup/IP_Camera_Cleanup.csproj new file mode 100644 index 0000000..9e2465a --- /dev/null +++ b/IP_Camera_Cleanup/IP_Camera_Cleanup.csproj @@ -0,0 +1,77 @@ + + + + + Debug + AnyCPU + {CCA56B64-383E-4707-87F5-39A4DD7BFD01} + WinExe + IP_Camera_Cleanup + IP_Camera_Cleanup + v4.5 + 512 + true + true + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + prompt + MinimumRecommendedRules.ruleset + true + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + prompt + MinimumRecommendedRules.ruleset + true + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/IP_Camera_Cleanup/Program.cs b/IP_Camera_Cleanup/Program.cs new file mode 100644 index 0000000..2f66326 --- /dev/null +++ b/IP_Camera_Cleanup/Program.cs @@ -0,0 +1,254 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; +using System.Diagnostics; + +namespace IP_Camera_Cleanup +{ + class Program + { + public enum ExitCodes + { + Success, + InvalidArgument = -1, + FailedRate = -2 + } + public static IEnumerable Exemptions; + + + static bool silent = false; + static void Main(string[] args) + { + string folderPath = ""; + string fileTypeString = "*.mp4"; + int daysOld = 30; + + if (args.Contains("\\f")) // Specify the folder path + { + int flagIndex = Array.IndexOf(args, "\\f"); + if(flagIndex + 1 < args.Length) + { + folderPath = args[flagIndex + 1]; + } + else + { + throw new ArgumentException("Missing or invalid source folder parameter."); + } + + } + if (args.Where((s) => s == "\\s").Count() > 0) + { + // run in silent mode + silent = true; + } + if (args.Contains("\\d")) // specify the age of the file in days + { + int flagIndex = Array.IndexOf(args, "\\d"); + if (flagIndex + 1 < args.Length && !args[flagIndex +1].StartsWith("\\")) + { + daysOld = int.Parse(args[flagIndex + 1]); + } + else + { + throw new ArgumentException("Missing or invalid file age parameter."); + } + } + if (args.Contains("\\t")) // specify the file type to search for + { + int flagIndex = Array.IndexOf(args, "\\t"); + if (flagIndex + 1 < args.Length && !args[flagIndex + 1].StartsWith("\\") && args[flagIndex + 1].StartsWith("*.")) + { + fileTypeString = args[flagIndex + 1]; + } + else + { + throw new ArgumentException("Missing or invalid file type parameter."); + } + } + //else + //{ + // Console.WriteLine("Invalid option. Valid options are: \'\\f\' {folder path}"); + // Console.WriteLine("Enter a folder path or press \'q\' to exit"); + // folderPath = Console.ReadLine(); + //} + if (folderPath == "q") + { + Environment.ExitCode = (int)ExitCodes.InvalidArgument; + } + else if(folderPath != "") + { + using (FileStream fs = File.Open(".\\Exemptions.lst", FileMode.OpenOrCreate, FileAccess.ReadWrite)) + { + byte[] exemptionsBytes = new byte[fs.Length]; + fs.Read(exemptionsBytes, 0, exemptionsBytes.Length); + Console.WriteLine("Exemptions file Loaded"); + Exemptions = Encoding.Unicode.GetString(exemptionsBytes, 0, exemptionsBytes.Length) + .Replace("\r\n", "\n").Split('\n').Where(x => x != ""); + } + // Hard-coded exemptions, to prevent bad behaviour + Exemptions = Exemptions.Concat(new List {"System Volume Information", "$RECYCLE.BIN", "C:\\Windows", "hiberfil.sys", "pagefile.sys", "swapfile.sys"}); + + foreach (var item in Exemptions) + { + Console.WriteLine(item); + } + + Result result = DeleteAllFiles(folderPath, daysOld, fileTypeString); + using (EventLog eventLog = new EventLog("Application")) + { + eventLog.Source = "Application"; + if (result.Percentage < 0.75f) // Exit with an error code if more than 25% of files failed + { + eventLog.WriteEntry(string.Format("File Cleanup failed to delete {0}% ({1}/{2}) files.", (int)((1f-result.Percentage)*100), result.Failed ,result.Count), EventLogEntryType.Warning); + Environment.Exit((int)ExitCodes.FailedRate); + } + else + { + eventLog.WriteEntry(string.Format("File Cleanup successfully deleted {0}/{1} files ({2}%)",result.Count-result.Failed,result.Count,(int)(result.Percentage * 100)), EventLogEntryType.Information); + Environment.Exit(0); + } + + } + + } + } + static Result DeleteAllFiles(string folderPath, int daysOld, string fileTypeString) + { + // Cleaner option for enumerating files + if (silent) + { + long count = 0; // Calling Count() on validFiles would force enumeration of all values. Light-weight method is to just record how many we've iterated through. We're in silent so logging the total files found can be retrospective + long failiures = 0; + + var rootDirs = Directory.EnumerateDirectories(folderPath, "*", SearchOption.TopDirectoryOnly).Where(x => Exemptions.All(e => !x.Contains(e))); + + foreach (var dir in rootDirs) + { + try + { + IEnumerable files = Directory.EnumerateFiles(dir, fileTypeString, SearchOption.AllDirectories).Where(x => IsDateOlder(x, daysOld)); + foreach (var file in files) + { + count++; + try + { + File.Delete(file); + } + catch + { + failiures++; + } + } + } + catch (Exception e) + { + using (EventLog eventLog = new EventLog("Application")) + { + eventLog.Source = "Application"; + eventLog.WriteEntry("Failed to enumerate files: " + e, EventLogEntryType.Error, 2222, 1); + } + } + } + if (failiures == 0) + { + return new Result() { Count = count, Failed = 0, Percentage = 1f }; + } + else + { + return new Result() { Count = count, Failed = failiures, Percentage = 1 - failiures / (float)count }; + } + } + else + { + // Old implementation is left here + List validFiles = FindFiles(folderPath).Where((s) => IsDateOlder(s, daysOld)).ToList(); + Console.WriteLine("Deleting {0} files...", validFiles.Count()); + int completeSize = validFiles.Count().ToString().Length; + int position = 0; + int failiures = 0; + foreach (string file in validFiles) + { + int Cursor_y = Console.CursorTop; + try + { + string line = new string(' ', completeSize - position.ToString().Length) + position; + Console.Write(line); + Console.SetCursorPosition(0, Cursor_y); + File.Delete(file); + position++; + } + catch (Exception e) + { + Console.WriteLine("Could not delete {0}: {1}", file, e); + position++; + failiures++; + } + } + if (failiures == 0) + { + return new Result() { Count = validFiles.Count, Failed = failiures, Percentage = 1f }; + } + else + { + return new Result() { Count = validFiles.Count, Failed = failiures, Percentage = 0f}; + } + } + + + } + static List FindFiles(string FolderPath) + { + List currentFound = new List(); + try + { + // Only enumerate files that aren't included in the exemptions + currentFound.AddRange(Directory.EnumerateFiles(FolderPath, "*.mp4", SearchOption.TopDirectoryOnly) + .Where((s) => Exemptions.All((e) => !s.Contains(e))) + .ToList()); + foreach (string path in Directory.EnumerateDirectories(FolderPath).Where((s) => Exemptions.All((e) => !s.Contains(e)))) + { + currentFound.AddRange(FindFiles(path)); + } + } + catch (Exception e) + { + using (EventLog eventLog = new EventLog("Application")) + { + eventLog.Source = "Application"; + eventLog.WriteEntry("Failed to enumerate files in " + FolderPath, EventLogEntryType.Error, 101, 1); + } + if (!silent) + { + Console.WriteLine(e); + } + } + + + return currentFound; + } + + static bool IsDateOlder(string fileName, int daysOld) + { + try + { + if (!Exemptions.Contains(fileName)) + { + FileInfo fileInfo = new FileInfo(fileName); + return (DateTime.UtcNow.Subtract(fileInfo.LastWriteTimeUtc) > new TimeSpan(daysOld, 0, 0, 0)); + } + + } + catch { } + return false; + } + } + class Result + { + public float Percentage { get; set; } + public long Count { get; set; } + public long Failed { get; set; } + } +} diff --git a/IP_Camera_Cleanup/Properties/AssemblyInfo.cs b/IP_Camera_Cleanup/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..e1923f1 --- /dev/null +++ b/IP_Camera_Cleanup/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("IP_Camera_Cleanup")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("IP_Camera_Cleanup")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("cca56b64-383e-4707-87f5-39a4dd7bfd01")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")]