Delete-By-Distinct-II
This commit is contained in:
4
Delete-By-Distinct/.vscode/launch.json
vendored
4
Delete-By-Distinct/.vscode/launch.json
vendored
@ -11,9 +11,7 @@
|
|||||||
"preLaunchTask": "build",
|
"preLaunchTask": "build",
|
||||||
// If you have changed target frameworks, make sure to update the program path.
|
// If you have changed target frameworks, make sure to update the program path.
|
||||||
"program": "${workspaceFolder}/bin/Debug/net7.0/win-x64/Delete-By-Distinct.dll",
|
"program": "${workspaceFolder}/bin/Debug/net7.0/win-x64/Delete-By-Distinct.dll",
|
||||||
"args": [
|
"args": [],
|
||||||
"s"
|
|
||||||
],
|
|
||||||
"env": {
|
"env": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development",
|
"ASPNETCORE_ENVIRONMENT": "Development",
|
||||||
},
|
},
|
||||||
|
@ -17,7 +17,6 @@ public class DeleteByDistinct
|
|||||||
{ }
|
{ }
|
||||||
if (console is null)
|
if (console is null)
|
||||||
{ }
|
{ }
|
||||||
string searchPattern = "*";
|
|
||||||
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, Dictionary<long, List<string>>> fileSizeToCollection = new();
|
||||||
@ -25,24 +24,29 @@ public class DeleteByDistinct
|
|||||||
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);
|
||||||
Work(searchPattern, ticks, log, configuration.RootDirectory, nameof(configuration.RootDirectory), options, fileSizeToCollection);
|
bool compareIsPopulatedAndNotTheSame = !string.IsNullOrEmpty(appSettings.CompareRootDirectory) && appSettings.CompareRootDirectory != configuration.RootDirectory;
|
||||||
Work(searchPattern, ticks, log, appSettings.CompareRootDirectory, nameof(appSettings.CompareRootDirectory), options, fileSizeToCollection);
|
Work(appSettings, ticks, log, configuration.RootDirectory, nameof(configuration.RootDirectory), options, fileSizeToCollection, logOnly: compareIsPopulatedAndNotTheSame);
|
||||||
|
if (compareIsPopulatedAndNotTheSame)
|
||||||
|
Work(appSettings, ticks, log, appSettings.CompareRootDirectory, nameof(appSettings.CompareRootDirectory), options, fileSizeToCollection, logOnly: false);
|
||||||
|
_ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(appSettings.CompareRootDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void Work(string searchPattern, long ticks, ILogger log, string directory, string variable, ProgressBarOptions options, Dictionary<long, Dictionary<long, List<string>>> fileSizeToCollection)
|
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)
|
||||||
{
|
{
|
||||||
string message;
|
string message;
|
||||||
string checkName;
|
string checkName;
|
||||||
string deleteLog;
|
string deleteLog;
|
||||||
int totalSeconds;
|
int totalSeconds;
|
||||||
FileInfo fileInfo;
|
FileInfo fileInfo;
|
||||||
|
DateTime checkDate;
|
||||||
ProgressBar progressBar;
|
ProgressBar progressBar;
|
||||||
List<string>? fileNames;
|
List<string>? fileNames;
|
||||||
|
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>>? fileTicksToNames;
|
||||||
log.Information($"Gathering files from <{directory}>");
|
log.Information($"Gathering {appSettings.SearchPattern} files from <{directory}>");
|
||||||
(string directory, string[] files)[] leftCollection = Shared.Models.Stateless.Methods.IFileHolder.GetFiles(directory, 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);
|
||||||
message = $") Looking for duplicates from <{directory}> - {totalSeconds} total second(s)";
|
message = $") Looking for duplicates from <{directory}> - {totalSeconds} total second(s)";
|
||||||
progressBar = new(leftCollection.Length, message, options);
|
progressBar = new(leftCollection.Length, message, options);
|
||||||
@ -52,42 +56,56 @@ public class DeleteByDistinct
|
|||||||
foreach (string file in files)
|
foreach (string file in files)
|
||||||
{
|
{
|
||||||
fileInfo = new(file);
|
fileInfo = new(file);
|
||||||
|
if (fileInfo.Length < 100)
|
||||||
|
continue;
|
||||||
if (!fileSizeToCollection.TryGetValue(fileInfo.Length, out fileTicksToNames))
|
if (!fileSizeToCollection.TryGetValue(fileInfo.Length, out fileTicksToNames))
|
||||||
{
|
{
|
||||||
fileSizeToCollection.Add(fileInfo.Length, new());
|
fileSizeToCollection.Add(fileInfo.Length, new());
|
||||||
if (!fileSizeToCollection.TryGetValue(fileInfo.Length, out fileTicksToNames))
|
if (!fileSizeToCollection.TryGetValue(fileInfo.Length, out fileTicksToNames))
|
||||||
throw new Exception();
|
throw new Exception();
|
||||||
}
|
}
|
||||||
if (!fileTicksToNames.TryGetValue(fileInfo.LastWriteTime.Ticks, out fileNames))
|
// if (!fileTicksToNames.TryGetValue(fileInfo.LastWriteTime.Ticks, out fileNames))
|
||||||
|
// {
|
||||||
|
// fileTicksToNames.Add(fileInfo.LastWriteTime.Ticks, new());
|
||||||
|
// if (!fileTicksToNames.TryGetValue(fileInfo.LastWriteTime.Ticks, out fileNames))
|
||||||
|
// throw new Exception();
|
||||||
|
// }
|
||||||
|
checkDate = new DateTime(fileInfo.LastWriteTime.Year, fileInfo.LastWriteTime.Month, fileInfo.LastWriteTime.Day);
|
||||||
|
if (!fileTicksToNames.TryGetValue(checkDate.Ticks, out fileNames))
|
||||||
{
|
{
|
||||||
fileTicksToNames.Add(fileInfo.LastWriteTime.Ticks, new());
|
fileTicksToNames.Add(checkDate.Ticks, new());
|
||||||
if (!fileTicksToNames.TryGetValue(fileInfo.LastWriteTime.Ticks, out fileNames))
|
if (!fileTicksToNames.TryGetValue(checkDate.Ticks, out fileNames))
|
||||||
throw new Exception();
|
throw new Exception();
|
||||||
}
|
}
|
||||||
checkName = fileInfo.Name.ToLower();
|
checkName = fileInfo.Name.ToLower(); //.Replace(".jpeg", ".jpg");
|
||||||
if (fileNames.Contains(checkName))
|
if (fileNames.Contains(checkName))
|
||||||
deletedFiles.Add(file);
|
deletedFiles.Add(file);
|
||||||
else
|
else
|
||||||
fileNames.Add(checkName);
|
fileNames.Add(checkName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
progressBar.Dispose();
|
||||||
|
log.Information(". . .");
|
||||||
deleteLog = $"{ticks}-{variable}-Files.lsv";
|
deleteLog = $"{ticks}-{variable}-Files.lsv";
|
||||||
|
if (!logOnly)
|
||||||
File.WriteAllLines(Path.Combine(directory, deleteLog), deletedFiles);
|
File.WriteAllLines(Path.Combine(directory, deleteLog), deletedFiles);
|
||||||
if (deletedFiles.Any())
|
if (deletedFiles.Any() && !logOnly)
|
||||||
{
|
{
|
||||||
log.Information($"Ready to delete {deletedFiles.Count} from {variable} file(s)? See <{deleteLog}>");
|
log.Information($"Ready to delete {deletedFiles.Count} from {variable} {appSettings.SearchPattern} file(s)? See <{deleteLog}>");
|
||||||
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) 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");
|
||||||
if (Console.ReadKey().Key == ConsoleKey.Y)
|
consoleKey = Console.ReadKey().Key;
|
||||||
|
if (consoleKey is ConsoleKey.Y or ConsoleKey.N)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
log.Information(". . .");
|
log.Information(". . .");
|
||||||
|
if (consoleKey is not null && consoleKey.Value == ConsoleKey.Y)
|
||||||
|
{
|
||||||
foreach (string file in deletedFiles)
|
foreach (string file in deletedFiles)
|
||||||
File.Delete(file);
|
File.Delete(file);
|
||||||
totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds);
|
totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds);
|
||||||
message = $") Looking for empty directories from <{directory}> - {totalSeconds} total second(s)";
|
message = $") Looking for empty directories from <{directory}> - {totalSeconds} total second(s)";
|
||||||
progressBar.Dispose();
|
|
||||||
progressBar = new(4, message, options);
|
progressBar = new(4, message, options);
|
||||||
for (int i = 1; i < 5; i++)
|
for (int i = 1; i < 5; i++)
|
||||||
{
|
{
|
||||||
@ -98,8 +116,11 @@ public class DeleteByDistinct
|
|||||||
break;
|
break;
|
||||||
deletedDirectories.AddRange(check);
|
deletedDirectories.AddRange(check);
|
||||||
}
|
}
|
||||||
deleteLog = $"{ticks}-{variable}-Directories.lsv";
|
progressBar.Dispose();
|
||||||
|
log.Information(". . .");
|
||||||
|
deleteLog = $"{ticks + 1}-{variable}-Directories.lsv";
|
||||||
File.WriteAllLines(Path.Combine(directory, deleteLog), deletedDirectories.Distinct());
|
File.WriteAllLines(Path.Combine(directory, deleteLog), deletedDirectories.Distinct());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -10,15 +10,17 @@ public class AppSettings
|
|||||||
public string CompareRootDirectory { init; get; }
|
public string CompareRootDirectory { init; get; }
|
||||||
public int MaxDegreeOfParallelism { init; get; }
|
public int MaxDegreeOfParallelism { init; get; }
|
||||||
public string OutputExtension { init; get; }
|
public string OutputExtension { init; get; }
|
||||||
|
public string SearchPattern { init; get; }
|
||||||
public string WorkingDirectoryName { init; get; }
|
public string WorkingDirectoryName { init; get; }
|
||||||
|
|
||||||
[JsonConstructor]
|
[JsonConstructor]
|
||||||
public AppSettings(string company, string compareRootDirectory, int maxDegreeOfParallelism, string outputExtension, string workingDirectoryName)
|
public AppSettings(string company, string compareRootDirectory, int maxDegreeOfParallelism, string outputExtension, string searchPattern, string workingDirectoryName)
|
||||||
{
|
{
|
||||||
Company = company;
|
Company = company;
|
||||||
CompareRootDirectory = compareRootDirectory;
|
CompareRootDirectory = compareRootDirectory;
|
||||||
MaxDegreeOfParallelism = maxDegreeOfParallelism;
|
MaxDegreeOfParallelism = maxDegreeOfParallelism;
|
||||||
OutputExtension = outputExtension;
|
OutputExtension = outputExtension;
|
||||||
|
SearchPattern = searchPattern;
|
||||||
WorkingDirectoryName = workingDirectoryName;
|
WorkingDirectoryName = workingDirectoryName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ public class AppSettings
|
|||||||
public string CompareRootDirectory { get; set; }
|
public string CompareRootDirectory { get; set; }
|
||||||
public int? MaxDegreeOfParallelism { get; set; }
|
public int? MaxDegreeOfParallelism { get; set; }
|
||||||
public string OutputExtension { get; set; }
|
public string OutputExtension { get; set; }
|
||||||
|
public string SearchPattern { get; set; }
|
||||||
public string WorkingDirectoryName { get; set; }
|
public string WorkingDirectoryName { get; set; }
|
||||||
|
|
||||||
#nullable restore
|
#nullable restore
|
||||||
@ -32,6 +33,7 @@ public class AppSettings
|
|||||||
appSettings.CompareRootDirectory,
|
appSettings.CompareRootDirectory,
|
||||||
appSettings.MaxDegreeOfParallelism.Value,
|
appSettings.MaxDegreeOfParallelism.Value,
|
||||||
appSettings.OutputExtension,
|
appSettings.OutputExtension,
|
||||||
|
appSettings.SearchPattern,
|
||||||
appSettings.WorkingDirectoryName
|
appSettings.WorkingDirectoryName
|
||||||
);
|
);
|
||||||
return result;
|
return result;
|
||||||
|
@ -7,13 +7,17 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"MaxDegreeOfParallelism": 6,
|
"MaxDegreeOfParallelism": 6,
|
||||||
|
"SearchPattern": "*.jpg",
|
||||||
"Serilog": {
|
"Serilog": {
|
||||||
"MinimumLevel": "Debug"
|
"MinimumLevel": "Debug"
|
||||||
},
|
},
|
||||||
"Windows": {
|
"Windows": {
|
||||||
"Configuration": {
|
"Configuration": {
|
||||||
"xRootDirectory": "C:/Tmp-x",
|
"xRootDirectory": "C:/Tmp-x",
|
||||||
"RootDirectory": "D:/Tmp/Phares/Compare/Not-Copy-Copy-4d49b68",
|
"xxRootDirectory": "D:/Tmp/Phares/Compare/Not-Copy-Copy-4d49b68",
|
||||||
|
"xxxRootDirectory": "F:/Tmp/Phares/Compare",
|
||||||
|
"xxxxRootDirectory": "E:/Tmp/Phares/Compare",
|
||||||
|
"RootDirectory": "F:/Tmp/Phares/Compare/Images-4d49b68",
|
||||||
"VerifyToSeason": [
|
"VerifyToSeason": [
|
||||||
". 2000",
|
". 2000",
|
||||||
". 2001",
|
". 2001",
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"MaxDegreeOfParallelism": 6,
|
"MaxDegreeOfParallelism": 6,
|
||||||
|
"SearchPattern": "*.jpg",
|
||||||
"Serilog": {
|
"Serilog": {
|
||||||
"Using": [
|
"Using": [
|
||||||
"Serilog.Sinks.Console",
|
"Serilog.Sinks.Console",
|
||||||
|
Reference in New Issue
Block a user