Offset-Date-Time-Original
Rename updates again Mirror-Length in place Delete-By-Distinct long to string Copy-Distinct handle move and move back
This commit is contained in:
@ -31,33 +31,58 @@ public class CopyDistinct
|
||||
_ConfigurationRoot = configurationRoot;
|
||||
ILogger? log = Log.ForContext<CopyDistinct>();
|
||||
Property.Models.Configuration propertyConfiguration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot);
|
||||
_FileGroups = Shared.Models.Stateless.Methods.IPath.GetKeyValuePairs(propertyConfiguration, appSettings.CopyTo, new string[] { appSettings.ResultDirectoryKey });
|
||||
Configuration configuration = Models.Binder.Configuration.Get(isEnvironment, configurationRoot, propertyConfiguration);
|
||||
_PropertyConfiguration = propertyConfiguration;
|
||||
_Configuration = configuration;
|
||||
propertyConfiguration.Update();
|
||||
log.Information(propertyConfiguration.RootDirectory);
|
||||
Verify();
|
||||
List<string> lines = CopyDistinctFilesInDirectories(log);
|
||||
File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", lines);
|
||||
if (!lines.Any())
|
||||
_ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(propertyConfiguration.RootDirectory);
|
||||
(bool move, List<string> allFiles, bool anyLenFiles, bool moveBack) = Verify();
|
||||
_FileGroups = Shared.Models.Stateless.Methods.IPath.GetKeyValuePairs(propertyConfiguration, appSettings.CopyTo, new string[] { appSettings.ResultDirectoryKey });
|
||||
List<string> lines = CopyDistinctFilesInDirectories(log, move, allFiles, anyLenFiles, moveBack);
|
||||
if (lines.Any())
|
||||
File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", lines);
|
||||
_ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(propertyConfiguration.RootDirectory);
|
||||
}
|
||||
|
||||
private void Verify()
|
||||
private (bool, List<string>, bool, bool) Verify()
|
||||
{
|
||||
if (_AppSettings is null)
|
||||
{ }
|
||||
throw new NullReferenceException(nameof(_AppSettings));
|
||||
if (_IsEnvironment is null)
|
||||
{ }
|
||||
throw new NullReferenceException(nameof(_IsEnvironment));
|
||||
if (_Configuration is null)
|
||||
{ }
|
||||
throw new NullReferenceException(nameof(_Configuration));
|
||||
if (_ConfigurationRoot is null)
|
||||
{ }
|
||||
throw new NullReferenceException(nameof(_ConfigurationRoot));
|
||||
if (_WorkingDirectory is null)
|
||||
{ }
|
||||
throw new NullReferenceException(nameof(_WorkingDirectory));
|
||||
if (_PropertyConfiguration is null)
|
||||
{ }
|
||||
throw new NullReferenceException(nameof(_PropertyConfiguration));
|
||||
bool moveBack;
|
||||
const string fileSearchFilter = "*";
|
||||
const string directorySearchFilter = "*";
|
||||
string copyTo = Path.GetFullPath(_AppSettings.CopyTo);
|
||||
bool move = copyTo == _PropertyConfiguration.RootDirectory;
|
||||
List<string[]> filesCollection = Shared.Models.Stateless.Methods.IDirectory.GetFilesCollection(_PropertyConfiguration.RootDirectory, directorySearchFilter, fileSearchFilter);
|
||||
(_, List<string> allFiles) = Get(filesCollection);
|
||||
bool anyLenFiles = allFiles.Any(l => l.EndsWith("len"));
|
||||
if (!move)
|
||||
moveBack = false;
|
||||
else
|
||||
{
|
||||
if (!anyLenFiles)
|
||||
throw new NotSupportedException("Use Mirror-Length app first!");
|
||||
if (string.IsNullOrEmpty(_AppSettings.ResultDirectoryKey))
|
||||
throw new NotSupportedException("Change appsettings!");
|
||||
if (!Directory.Exists(Path.Combine(_PropertyConfiguration.RootDirectory, _AppSettings.ResultDirectoryKey)))
|
||||
moveBack = false;
|
||||
else
|
||||
{
|
||||
move = false;
|
||||
moveBack = true;
|
||||
}
|
||||
}
|
||||
return (move, allFiles, anyLenFiles, moveBack);
|
||||
}
|
||||
|
||||
private static (List<string>, List<string>) Get(List<string[]> filesCollection)
|
||||
@ -89,15 +114,16 @@ public class CopyDistinct
|
||||
string directoryName;
|
||||
bool wrapped = false;
|
||||
List<string> distinct = new();
|
||||
FileHolder[] sortedFileHolders = (from l in fileHolders orderby l.LastWriteTime, l.FullName.Length descending select l).ToArray();
|
||||
string key = string.IsNullOrEmpty(_AppSettings.ResultDirectoryKey) ? _PropertyConfiguration.ResultAllInOne : _AppSettings.ResultDirectoryKey;
|
||||
foreach (FileHolder fileHolder in fileHolders)
|
||||
foreach (FileHolder fileHolder in sortedFileHolders)
|
||||
{
|
||||
progressBar.Tick();
|
||||
if (fileHolder.ExtensionLowered == ".id" || fileHolder.ExtensionLowered == ".lsv" || fileHolder.DirectoryName is null)
|
||||
if (fileHolder.Name.EndsWith("len") || fileHolder.ExtensionLowered == ".id" || fileHolder.ExtensionLowered == ".lsv" || fileHolder.DirectoryName is null)
|
||||
continue;
|
||||
(_, directoryIndex) = Shared.Models.Stateless.Methods.IPath.GetDirectoryNameAndIndex(_PropertyConfiguration.ResultAllInOneSubdirectoryLength, fileHolder.NameWithoutExtension);
|
||||
directoryName = Path.GetFileName(fileHolder.DirectoryName);
|
||||
if (directoryName.Length < _PropertyConfiguration.ResultAllInOneSubdirectoryLength || !fileHolder.Name.StartsWith(directoryName))
|
||||
if (directoryName.Length < _PropertyConfiguration.ResultAllInOneSubdirectoryLength + 3 || !fileHolder.Name.StartsWith(directoryName))
|
||||
{
|
||||
if (wrapped)
|
||||
continue;
|
||||
@ -135,7 +161,64 @@ public class CopyDistinct
|
||||
return results;
|
||||
}
|
||||
|
||||
private static List<string> Copy(ProgressBar progressBar, List<(FileHolder, string, string)> toDoCollection)
|
||||
private static List<(FileHolder, string, string)> GetMoveBackToDoCollection(List<string> files)
|
||||
{
|
||||
List<(FileHolder, string, string)> results = new();
|
||||
string key;
|
||||
string? value;
|
||||
string fileName;
|
||||
files.Reverse();
|
||||
string? directory;
|
||||
string destinationFile;
|
||||
List<string> distinctFound = new();
|
||||
List<string> distinctNeeded = new();
|
||||
Dictionary<string, string> nameToPath = new();
|
||||
for (int i = 1; i < 3; i++)
|
||||
{
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileName = Path.GetFileName(file);
|
||||
if (fileName.EndsWith("len"))
|
||||
{
|
||||
key = fileName[..^3];
|
||||
destinationFile = file[..^3];
|
||||
if (nameToPath.ContainsKey(key))
|
||||
continue;
|
||||
nameToPath.Add(key, destinationFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!distinctNeeded.Contains(file))
|
||||
distinctNeeded.Add(file);
|
||||
if (!nameToPath.ContainsKey(fileName))
|
||||
continue;
|
||||
if (distinctFound.Contains(file))
|
||||
continue;
|
||||
distinctFound.Add(file);
|
||||
}
|
||||
}
|
||||
if (distinctNeeded.Count != distinctFound.Count)
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
foreach (string file in files)
|
||||
{
|
||||
// if (distinctNeeded.Count != distinctFound.Count)
|
||||
// continue;
|
||||
fileName = Path.GetFileName(file);
|
||||
if (fileName.EndsWith("len"))
|
||||
continue;
|
||||
if (!nameToPath.TryGetValue(fileName, out value))
|
||||
continue;
|
||||
directory = Path.GetDirectoryName(value);
|
||||
if (string.IsNullOrEmpty(directory))
|
||||
continue;
|
||||
results.Add(new(new(file), directory, value));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private static List<string> CopyOrMove(ProgressBar progressBar, List<(FileHolder, string, string)> toDoCollection, bool move, bool moveBack)
|
||||
{
|
||||
List<string> results = new();
|
||||
FileInfo fileInfo;
|
||||
@ -151,28 +234,39 @@ public class CopyDistinct
|
||||
}
|
||||
results.Add(fileHolder.NameWithoutExtension);
|
||||
try
|
||||
{ File.Copy(fileHolder.FullName, to); }
|
||||
{
|
||||
if (move || moveBack)
|
||||
File.Move(fileHolder.FullName, to);
|
||||
else
|
||||
File.Copy(fileHolder.FullName, to);
|
||||
}
|
||||
catch (Exception) { }
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private List<string> CopyDistinctFilesInDirectories(ILogger log)
|
||||
private List<string> CopyDistinctFilesInDirectories(ILogger log, bool move, List<string> allFiles, bool anyLenFiles, bool moveBack)
|
||||
{
|
||||
List<string> results = new();
|
||||
ProgressBar progressBar;
|
||||
ConsoleKey? consoleKey = null;
|
||||
const string fileSearchFilter = "*";
|
||||
string message = nameof(CopyDistinct);
|
||||
const string directorySearchFilter = "*";
|
||||
List<string> distinctDirectories = new();
|
||||
List<string[]> filesCollection = Shared.Models.Stateless.Methods.IDirectory.GetFilesCollection(_PropertyConfiguration.RootDirectory, directorySearchFilter, fileSearchFilter);
|
||||
(_, List<string> allFiles) = Get(filesCollection);
|
||||
List<(FileHolder, string, string)> toDoCollection;
|
||||
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
|
||||
progressBar = new(allFiles.Count, message, options);
|
||||
FileHolder[] fileHolders = (from l in allFiles select new FileHolder(l)).OrderBy(l => l.LastWriteTime).ToArray();
|
||||
List<(FileHolder, string, string)> toDoCollection = GetToDoCollection(progressBar, fileHolders);
|
||||
progressBar.Dispose();
|
||||
if (moveBack)
|
||||
{
|
||||
if (!anyLenFiles)
|
||||
throw new NotSupportedException();
|
||||
toDoCollection = GetMoveBackToDoCollection(allFiles);
|
||||
}
|
||||
else
|
||||
{
|
||||
progressBar = new(allFiles.Count, message, options);
|
||||
FileHolder[] fileHolders = (from l in allFiles select new FileHolder(l)).ToArray();
|
||||
toDoCollection = GetToDoCollection(progressBar, fileHolders);
|
||||
progressBar.Dispose();
|
||||
}
|
||||
foreach ((FileHolder fileHolder, string directory, string to) in toDoCollection)
|
||||
{
|
||||
if (distinctDirectories.Contains(directory))
|
||||
@ -184,23 +278,41 @@ public class CopyDistinct
|
||||
if (!Directory.Exists(distinctDirectory))
|
||||
_ = Directory.CreateDirectory(distinctDirectory);
|
||||
}
|
||||
log.Information($"Ready to Copy {toDoCollection.Count} file(s)?");
|
||||
if (move)
|
||||
log.Information($"Ready to Move {toDoCollection.Count} file(s)?");
|
||||
else if (!moveBack)
|
||||
log.Information($"Ready to Copy {toDoCollection.Count} file(s)?");
|
||||
else
|
||||
log.Information($"Ready to Move back {toDoCollection.Count} file(s)?");
|
||||
for (int y = 0; y < int.MaxValue; y++)
|
||||
{
|
||||
log.Information("Press \"Y\" key to copy file(s), \"N\" key to log file(s) or close console to not copy files");
|
||||
if (move)
|
||||
log.Information("Press \"Y\" key to move file(s), \"N\" key to log file(s) or close console to not move files");
|
||||
else if (!moveBack)
|
||||
log.Information("Press \"Y\" key to copy file(s), \"N\" key to log file(s) or close console to not copy files");
|
||||
else
|
||||
log.Information("Press \"Y\" key to move back file(s), \"N\" key to log file(s) or close console to not move back files");
|
||||
consoleKey = System.Console.ReadKey().Key;
|
||||
if (consoleKey is ConsoleKey.Y or ConsoleKey.N)
|
||||
break;
|
||||
}
|
||||
log.Information(". . .");
|
||||
if (consoleKey is null || consoleKey.Value != ConsoleKey.Y)
|
||||
log.Information("Nothing copied!");
|
||||
{
|
||||
if (move || moveBack)
|
||||
log.Information("Nothing moved!");
|
||||
else
|
||||
log.Information("Nothing copied!");
|
||||
}
|
||||
else
|
||||
{
|
||||
progressBar = new(allFiles.Count, message, options);
|
||||
results.AddRange(Copy(progressBar, toDoCollection));
|
||||
results.AddRange(CopyOrMove(progressBar, toDoCollection, move, moveBack));
|
||||
progressBar.Dispose();
|
||||
log.Information("Done copying");
|
||||
if (move || moveBack)
|
||||
log.Information("Done moving");
|
||||
else
|
||||
log.Information("Done copying");
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
Reference in New Issue
Block a user