Delete-By-Distinct appSettings changes

This commit is contained in:
Mike Phares 2023-06-10 17:13:33 -07:00
parent cb891b4fcf
commit bdd2a637ec
6 changed files with 90 additions and 77 deletions

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion> <LangVersion>10.0</LangVersion>
@ -6,7 +6,8 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<RuntimeIdentifier>win-x64</RuntimeIdentifier> <RuntimeIdentifier>win-x64</RuntimeIdentifier>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
</PropertyGroup> <UserSecretsId>0589ecff-b296-48be-a3f7-7bf27f453975</UserSecretsId>
</PropertyGroup>
<PropertyGroup> <PropertyGroup>
<PackageId>Phares.View.by.Distance.Delete.By.Distinct</PackageId> <PackageId>Phares.View.by.Distance.Delete.By.Distinct</PackageId>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild> <GeneratePackageOnBuild>false</GeneratePackageOnBuild>

View File

@ -19,25 +19,24 @@ public class DeleteByDistinct
{ } { }
long ticks = DateTime.Now.Ticks; long ticks = DateTime.Now.Ticks;
ILogger? log = Log.ForContext<DeleteByDistinct>(); ILogger? log = Log.ForContext<DeleteByDistinct>();
Dictionary<long, Dictionary<long, List<string>>> fileSizeToCollection = new(); Dictionary<long, List<string>> longToCollection = new();
Configuration configuration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot); Configuration configuration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot);
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true }; ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
configuration.Update(); configuration.Update();
log.Information(configuration.RootDirectory); log.Information(configuration.RootDirectory);
bool compareIsPopulatedAndNotTheSame = !string.IsNullOrEmpty(appSettings.CompareRootDirectory) && appSettings.CompareRootDirectory != configuration.RootDirectory; bool compareIsPopulatedAndNotTheSame = !string.IsNullOrEmpty(appSettings.CompareRootDirectory) && appSettings.CompareRootDirectory != configuration.RootDirectory;
Work(appSettings, ticks, log, configuration.RootDirectory, nameof(configuration.RootDirectory), options, fileSizeToCollection, logOnly: compareIsPopulatedAndNotTheSame); Work(appSettings, ticks, log, configuration.RootDirectory, nameof(configuration.RootDirectory), options, longToCollection, logOnly: compareIsPopulatedAndNotTheSame);
if (compareIsPopulatedAndNotTheSame) if (compareIsPopulatedAndNotTheSame)
Work(appSettings, ticks, log, appSettings.CompareRootDirectory, nameof(appSettings.CompareRootDirectory), options, fileSizeToCollection, logOnly: false); Work(appSettings, ticks, log, appSettings.CompareRootDirectory, nameof(appSettings.CompareRootDirectory), options, longToCollection, logOnly: false);
_ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(appSettings.CompareRootDirectory); _ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(appSettings.CompareRootDirectory);
} }
private static void Work(AppSettings appSettings, long ticks, ILogger log, string directory, string variable, ProgressBarOptions options, Dictionary<long, Dictionary<long, List<string>>> fileSizeToCollection, bool logOnly) private static void Work(AppSettings appSettings, long ticks, ILogger log, string directory, string variable, ProgressBarOptions options, Dictionary<long, List<string>> longToCollection, bool logOnly)
{ {
long check;
string message; string message;
long checkTicks; string logFile;
long checkLength;
string checkName; string checkName;
string deleteLog;
int totalSeconds; int totalSeconds;
FileInfo fileInfo; FileInfo fileInfo;
ProgressBar progressBar; ProgressBar progressBar;
@ -45,7 +44,8 @@ public class DeleteByDistinct
ConsoleKey? consoleKey = null; ConsoleKey? consoleKey = null;
List<string> deletedFiles = new(); List<string> deletedFiles = new();
List<string> deletedDirectories = new(); List<string> deletedDirectories = new();
Dictionary<long, List<string>>? fileTicksToNames; Dictionary<long, List<string>> longToCollectionB = new();
List<(string Source, string Destination)> renameFiles = new();
log.Information($"Gathering {appSettings.SearchPattern} files from <{directory}>"); log.Information($"Gathering {appSettings.SearchPattern} files from <{directory}>");
(string directory, string[] files)[] leftCollection = Shared.Models.Stateless.Methods.IFileHolder.GetFiles(directory, appSettings.SearchPattern).ToArray(); (string directory, string[] files)[] leftCollection = Shared.Models.Stateless.Methods.IFileHolder.GetFiles(directory, appSettings.SearchPattern).ToArray();
totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds); totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds);
@ -56,33 +56,33 @@ public class DeleteByDistinct
progressBar.Tick(); progressBar.Tick();
foreach (string file in files) foreach (string file in files)
{ {
if (file.EndsWith(".id") || file.Contains("Rename")) if (file.EndsWith(".id") || file.Contains("Rename") || file.EndsWith(".lsv"))
continue; continue;
fileInfo = new(file); fileInfo = new(file);
if (fileInfo.Length < 100) if (appSettings.SizeForLong)
continue;
if (appSettings.RecycleOption)
checkLength = 1;
else
checkLength = fileInfo.Length;
if (!fileSizeToCollection.TryGetValue(checkLength, out fileTicksToNames))
{ {
fileSizeToCollection.Add(checkLength, new()); if (fileInfo.Length < 100)
if (!fileSizeToCollection.TryGetValue(checkLength, out fileTicksToNames)) continue;
throw new Exception(); check = fileInfo.Length;
} }
if (appSettings.RecycleOption) else if (appSettings.TicksForLong)
checkTicks = 1;
else
checkTicks = new DateTime(ticks).Ticks;
if (!fileTicksToNames.TryGetValue(checkTicks, out fileNames))
{ {
fileTicksToNames.Add(checkTicks, new()); if (fileInfo.LastWriteTime.Hour == 0 && fileInfo.LastWriteTime.Minute == 0 && fileInfo.LastWriteTime.Second == 0)
if (!fileTicksToNames.TryGetValue(checkTicks, out fileNames)) continue;
check = fileInfo.LastWriteTime.Ticks;
}
else
throw new Exception();
if (!longToCollection.TryGetValue(check, out fileNames))
{
longToCollection.Add(check, new());
if (!longToCollection.TryGetValue(check, out fileNames))
throw new Exception(); throw new Exception();
} }
checkName = fileInfo.Name.ToLower().Replace(".jpeg", ".jpg"); checkName = fileInfo.Name.ToLower().Replace(".jpeg", ".jpg");
if (fileNames.Contains(checkName)) if (!logOnly && appSettings.RenameToMatch && fileNames.Count == 1 && fileInfo.DirectoryName is not null && fileInfo.Name != fileNames.First())
renameFiles.Add((fileInfo.FullName, Path.Combine(fileInfo.DirectoryName, fileNames.First())));
else if (fileNames.Contains(checkName))
deletedFiles.Add(file); deletedFiles.Add(file);
else else
fileNames.Add(checkName); fileNames.Add(checkName);
@ -90,12 +90,12 @@ public class DeleteByDistinct
} }
progressBar.Dispose(); progressBar.Dispose();
log.Information(". . ."); log.Information(". . .");
deleteLog = $"{ticks}-{variable}-Files.lsv"; logFile = $"{ticks}-{variable}-Files-A.lsv";
if (!logOnly) if (!logOnly)
File.WriteAllLines(Path.Combine(directory, deleteLog), deletedFiles); File.WriteAllLines(Path.Combine(directory, logFile), deletedFiles);
if (deletedFiles.Any() && !logOnly) if (deletedFiles.Any() && !logOnly)
{ {
log.Information($"Ready to delete {deletedFiles.Count} from {variable} {appSettings.SearchPattern} file(s)? See <{deleteLog}>"); log.Information($"Ready to delete {deletedFiles.Count} from {variable} {appSettings.SearchPattern} file(s)? See <{logFile}>");
for (int y = 0; y < int.MaxValue; y++) for (int y = 0; y < int.MaxValue; y++)
{ {
log.Information("Press \"Y\" key to delete file(s), \"N\" key to log file(s) or close console to not delete files"); log.Information("Press \"Y\" key to delete file(s), \"N\" key to log file(s) or close console to not delete files");
@ -127,16 +127,40 @@ public class DeleteByDistinct
for (int i = 1; i < 5; i++) for (int i = 1; i < 5; i++)
{ {
progressBar.Tick(); progressBar.Tick();
List<string> check = new(); List<string> collection = new();
Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(directory, check); Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(directory, collection);
if (!check.Any()) if (!collection.Any())
break; break;
deletedDirectories.AddRange(check); deletedDirectories.AddRange(collection);
} }
progressBar.Dispose(); progressBar.Dispose();
log.Information(". . ."); log.Information(". . .");
deleteLog = $"{ticks + 1}-{variable}-Directories.lsv"; logFile = $"{ticks + 1}-{variable}-Directories.lsv";
File.WriteAllLines(Path.Combine(directory, deleteLog), deletedDirectories.Distinct()); File.WriteAllLines(Path.Combine(directory, logFile), deletedDirectories.Distinct());
}
}
logFile = $"{ticks}-{variable}-Files-B.lsv";
if (!logOnly)
File.WriteAllLines(Path.Combine(directory, logFile), renameFiles.Select(l => l.Source));
if (renameFiles.Any() && !logOnly)
{
log.Information($"Ready to rename to match {renameFiles.Count} from {variable} {appSettings.SearchPattern} file(s)? See <{logFile}>");
for (int y = 0; y < int.MaxValue; y++)
{
log.Information("Press \"Y\" key to rename to match file(s), \"N\" key to log file(s) or close console to not rename to match files");
consoleKey = Console.ReadKey().Key;
if (consoleKey is ConsoleKey.Y or ConsoleKey.N)
break;
}
log.Information(". . .");
if (consoleKey is not null && consoleKey.Value == ConsoleKey.Y)
{
foreach ((string source, string destination) in renameFiles)
{
try
{ File.Move(source, destination); }
catch (Exception) { }
}
} }
} }
} }

View File

@ -1,37 +1,19 @@
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization;
namespace View_by_Distance.Delete.By.Distinct.Models; namespace View_by_Distance.Delete.By.Distinct.Models;
public class AppSettings public record AppSettings(string Company,
string CompareRootDirectory,
int MaxDegreeOfParallelism,
string OutputExtension,
bool RecycleOption,
bool RenameToMatch,
string SearchPattern,
bool SizeForLong,
bool TicksForLong,
string WorkingDirectoryName)
{ {
public string Company { init; get; }
public string CompareRootDirectory { init; get; }
public int MaxDegreeOfParallelism { init; get; }
public string OutputExtension { init; get; }
public bool RecycleOption { init; get; }
public string SearchPattern { init; get; }
public string WorkingDirectoryName { init; get; }
[JsonConstructor]
public AppSettings(string company,
string compareRootDirectory,
int maxDegreeOfParallelism,
string outputExtension,
bool recycleOption,
string searchPattern,
string workingDirectoryName)
{
Company = company;
CompareRootDirectory = compareRootDirectory;
MaxDegreeOfParallelism = maxDegreeOfParallelism;
OutputExtension = outputExtension;
RecycleOption = recycleOption;
SearchPattern = searchPattern;
WorkingDirectoryName = workingDirectoryName;
}
public override string ToString() public override string ToString()
{ {
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });

View File

@ -13,7 +13,10 @@ public class AppSettings
public int? MaxDegreeOfParallelism { get; set; } public int? MaxDegreeOfParallelism { get; set; }
public string OutputExtension { get; set; } public string OutputExtension { get; set; }
public bool? RecycleOption { get; set; } public bool? RecycleOption { get; set; }
public bool? RenameToMatch { get; set; }
public string SearchPattern { get; set; } public string SearchPattern { get; set; }
public bool? SizeForLong { get; set; }
public bool? TicksForLong { get; set; }
public string WorkingDirectoryName { get; set; } public string WorkingDirectoryName { get; set; }
#nullable restore #nullable restore
@ -31,13 +34,24 @@ public class AppSettings
throw new NullReferenceException(nameof(appSettings.MaxDegreeOfParallelism)); throw new NullReferenceException(nameof(appSettings.MaxDegreeOfParallelism));
if (appSettings?.RecycleOption is null) if (appSettings?.RecycleOption is null)
throw new NullReferenceException(nameof(appSettings.RecycleOption)); throw new NullReferenceException(nameof(appSettings.RecycleOption));
if (appSettings?.RenameToMatch is null)
throw new NullReferenceException(nameof(appSettings.RenameToMatch));
if (appSettings?.SizeForLong is null)
throw new NullReferenceException(nameof(appSettings.SizeForLong));
if (appSettings?.TicksForLong is null)
throw new NullReferenceException(nameof(appSettings.TicksForLong));
if (appSettings.TicksForLong.Value == appSettings.SizeForLong.Value)
throw new Exception("Check appSettings file!");
result = new( result = new(
appSettings.Company, appSettings.Company,
appSettings.CompareRootDirectory, appSettings.CompareRootDirectory,
appSettings.MaxDegreeOfParallelism.Value, appSettings.MaxDegreeOfParallelism.Value,
appSettings.OutputExtension, appSettings.OutputExtension,
appSettings.RecycleOption.Value, appSettings.RecycleOption.Value,
appSettings.RenameToMatch.Value,
appSettings.SearchPattern, appSettings.SearchPattern,
appSettings.SizeForLong.Value,
appSettings.TicksForLong.Value,
appSettings.WorkingDirectoryName appSettings.WorkingDirectoryName
); );
return result; return result;

View File

@ -1,25 +1,14 @@
{ {
"CompareRootDirectory": "D:/2) Images B/Not-Copy-Copy-1e85c0ba",
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Log4netProvider": "Debug" "Log4netProvider": "Debug"
} }
}, },
"MaxDegreeOfParallelism": 6,
"RecycleOption": true,
"SearchPattern": "*.*",
"Serilog": { "Serilog": {
"MinimumLevel": "Debug" "MinimumLevel": "Debug"
}, },
"Windows": { "Windows": {
"Configuration": { "Configuration": {
"xRootDirectory": "D:/2) Images B/Corrupt",
"xxRootDirectory": "D:/2) Images B/Not-Copy-Copy-1e85c0ba",
"RootDirectory": "D:/1) Images A/Images-1e85c0ba",
"xxxxRootDirectory": "E:/3) Videos A/Device Videos 2_0_0_3 - Current",
"xxxxxRootDirectory": "E:/4) Videos B/Device Videos 2_0_0_3 - Current - Ignore",
"xxxxxxRootDirectory": "C:/1) Images A/Images-1e85c0ba",
"xxxxxxxRootDirectory": "C:/2) Images B/Not-Copy-Copy-1e85c0ba",
"VerifyToSeason": [] "VerifyToSeason": []
} }
} }

View File

@ -12,7 +12,10 @@
}, },
"MaxDegreeOfParallelism": 6, "MaxDegreeOfParallelism": 6,
"RecycleOption": false, "RecycleOption": false,
"RenameToMatch": false,
"SizeForLong": true,
"SearchPattern": "*.jpg", "SearchPattern": "*.jpg",
"TicksForLong": false,
"Serilog": { "Serilog": {
"Using": [ "Using": [
"Serilog.Sinks.Console", "Serilog.Sinks.Console",