Added solution files
This commit is contained in:
parent
9b0a10f78b
commit
3682a80790
31
IP_Camera_Cleanup.sln
Normal file
31
IP_Camera_Cleanup.sln
Normal file
@ -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
|
6
IP_Camera_Cleanup/App.config
Normal file
6
IP_Camera_Cleanup/App.config
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<configuration>
|
||||||
|
<startup>
|
||||||
|
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
|
||||||
|
</startup>
|
||||||
|
</configuration>
|
77
IP_Camera_Cleanup/IP_Camera_Cleanup.csproj
Normal file
77
IP_Camera_Cleanup/IP_Camera_Cleanup.csproj
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||||
|
<PropertyGroup>
|
||||||
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
|
<ProjectGuid>{CCA56B64-383E-4707-87F5-39A4DD7BFD01}</ProjectGuid>
|
||||||
|
<OutputType>WinExe</OutputType>
|
||||||
|
<RootNamespace>IP_Camera_Cleanup</RootNamespace>
|
||||||
|
<AssemblyName>IP_Camera_Cleanup</AssemblyName>
|
||||||
|
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||||
|
<FileAlignment>512</FileAlignment>
|
||||||
|
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||||
|
<Deterministic>true</Deterministic>
|
||||||
|
<TargetFrameworkProfile />
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
|
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||||
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
<Optimize>false</Optimize>
|
||||||
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
|
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||||
|
<DebugType>pdbonly</DebugType>
|
||||||
|
<Optimize>true</Optimize>
|
||||||
|
<OutputPath>bin\Release\</OutputPath>
|
||||||
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
|
||||||
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
<OutputPath>bin\x64\Debug\</OutputPath>
|
||||||
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
<PlatformTarget>x64</PlatformTarget>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||||
|
<Prefer32Bit>true</Prefer32Bit>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
|
||||||
|
<OutputPath>bin\x64\Release\</OutputPath>
|
||||||
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
|
<Optimize>true</Optimize>
|
||||||
|
<DebugType>pdbonly</DebugType>
|
||||||
|
<PlatformTarget>x64</PlatformTarget>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||||
|
<Prefer32Bit>true</Prefer32Bit>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup>
|
||||||
|
<StartupObject />
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.Core" />
|
||||||
|
<Reference Include="System.Xml.Linq" />
|
||||||
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
|
<Reference Include="Microsoft.CSharp" />
|
||||||
|
<Reference Include="System.Data" />
|
||||||
|
<Reference Include="System.Net.Http" />
|
||||||
|
<Reference Include="System.Xml" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="Program.cs" />
|
||||||
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="App.config" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
</Project>
|
254
IP_Camera_Cleanup/Program.cs
Normal file
254
IP_Camera_Cleanup/Program.cs
Normal file
@ -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<string> 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<string> {"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<string> 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<string> 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<string> FindFiles(string FolderPath)
|
||||||
|
{
|
||||||
|
List<string> currentFound = new List<string>();
|
||||||
|
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; }
|
||||||
|
}
|
||||||
|
}
|
36
IP_Camera_Cleanup/Properties/AssemblyInfo.cs
Normal file
36
IP_Camera_Cleanup/Properties/AssemblyInfo.cs
Normal file
@ -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")]
|
Loading…
x
Reference in New Issue
Block a user