Rename questions

This commit is contained in:
Mike Phares 2023-02-05 10:53:03 -07:00
parent b28db0abab
commit f380a7ec20
2 changed files with 65 additions and 21 deletions

View File

@ -108,8 +108,6 @@ public class Configuration : Shared.Models.Properties.IPropertyConfiguration
throw new NullReferenceException(nameof(propertyConfiguration.ValidMetadataExtensions)); throw new NullReferenceException(nameof(propertyConfiguration.ValidMetadataExtensions));
if (propertyConfiguration.VerifyToSeason is null || !propertyConfiguration.VerifyToSeason.Any()) if (propertyConfiguration.VerifyToSeason is null || !propertyConfiguration.VerifyToSeason.Any())
throw new NullReferenceException(nameof(propertyConfiguration.VerifyToSeason)); throw new NullReferenceException(nameof(propertyConfiguration.VerifyToSeason));
if (Path.GetPathRoot(propertyConfiguration.RootDirectory) == propertyConfiguration.RootDirectory)
throw new NullReferenceException(nameof(propertyConfiguration.RootDirectory));
if (propertyConfiguration is null) if (propertyConfiguration is null)
throw new NullReferenceException(nameof(propertyConfiguration)); throw new NullReferenceException(nameof(propertyConfiguration));
if (string.IsNullOrEmpty(propertyConfiguration.DateGroup)) if (string.IsNullOrEmpty(propertyConfiguration.DateGroup))

View File

@ -52,7 +52,7 @@ public class Rename
Rename2000(matchNginxCollection); Rename2000(matchNginxCollection);
else if (matchNginxCollection.Any()) else if (matchNginxCollection.Any())
{ {
List<string> lines = RenameFilesInDirectories(options, matchNginxCollection); List<string> lines = RenameFilesInDirectories(log, options, matchNginxCollection);
File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", lines); File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", lines);
if (comparePathRoot != Path.GetPathRoot(matchNginxCollection[0].ConvertedPath)) if (comparePathRoot != Path.GetPathRoot(matchNginxCollection[0].ConvertedPath))
_ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(comparePathRoot); _ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(comparePathRoot);
@ -261,13 +261,15 @@ public class Rename
} }
} }
private void CopyInstead(List<(FileHolder, string)> toDoCollection) private List<string> CopyInstead(ILogger log, List<(FileHolder, string)> verifiedToDoCollection)
{ {
List<string> results = new();
string copyTo; string copyTo;
string? directory; string? directory;
ConsoleKey? consoleKey = null;
List<string> distinctDirectories = new(); List<string> distinctDirectories = new();
List<(FileHolder, string)> copyCollection = new(); List<(FileHolder, string)> copyCollection = new();
foreach ((FileHolder fileHolder, string to) in toDoCollection) foreach ((FileHolder fileHolder, string to) in verifiedToDoCollection)
{ {
copyTo = $"{_AppSettings.CopyTo}{to[1..]}"; copyTo = $"{_AppSettings.CopyTo}{to[1..]}";
directory = Path.GetDirectoryName(copyTo); directory = Path.GetDirectoryName(copyTo);
@ -279,15 +281,58 @@ public class Rename
distinctDirectories.Add(directory); distinctDirectories.Add(directory);
} }
CreateDirectories(distinctDirectories); CreateDirectories(distinctDirectories);
foreach ((FileHolder fileHolder, string to) in copyCollection) log.Information($"Ready to Copy {verifiedToDoCollection.Count} file(s)?");
for (int y = 0; y < int.MaxValue; y++)
{ {
if (File.Exists(to)) log.Information("Press \"Y\" key to copy file(s), \"N\" key to log file(s) or close console to not copy files");
continue; consoleKey = System.Console.ReadKey().Key;
File.Copy(fileHolder.FullName, to); if (consoleKey is ConsoleKey.Y or ConsoleKey.N)
break;
} }
log.Information(". . .");
if (consoleKey is null || consoleKey.Value != ConsoleKey.Y)
log.Information("Nothing Copied!");
else
{
foreach ((FileHolder fileHolder, string to) in verifiedToDoCollection)
{
results.Add(fileHolder.NameWithoutExtension);
File.Copy(fileHolder.FullName, to);
}
log.Information("Done Copying");
}
return results;
} }
private List<string> RenameFilesInDirectories(ProgressBarOptions options, MatchNginx[] matchNginxCollection) private static List<string> Move(ILogger log, List<(FileHolder, string)> verifiedToDoCollection)
{
List<string> results = new();
ConsoleKey? consoleKey = null;
log.Information($"Ready to Move {verifiedToDoCollection.Count} file(s)?");
for (int y = 0; y < int.MaxValue; y++)
{
log.Information("Press \"Y\" key to move file(s), \"N\" key to log file(s) or close console to not move 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 moved!");
else
{
foreach ((FileHolder fileHolder, string to) in verifiedToDoCollection)
{
results.Add(fileHolder.NameWithoutExtension);
File.Move(fileHolder.FullName, to);
File.WriteAllText($"{to}.id", $"{to}{Environment.NewLine}{fileHolder.FullName}");
}
log.Information("Done Moving");
}
return results;
}
private List<string> RenameFilesInDirectories(ILogger log, ProgressBarOptions options, MatchNginx[] matchNginxCollection)
{ {
List<string> results = new(); List<string> results = new();
string[] files; string[] files;
@ -296,6 +341,7 @@ public class Rename
ProgressBar progressBar; ProgressBar progressBar;
List<string> distinctCollection = new(); List<string> distinctCollection = new();
List<(FileHolder, string)> toDoCollection; List<(FileHolder, string)> toDoCollection;
List<(FileHolder, string)> verifiedToDoCollection;
List<string> allFiles = GetAllFiles(matchNginxCollection); List<string> allFiles = GetAllFiles(matchNginxCollection);
for (int i = 1; i < 3; i++) for (int i = 1; i < 3; i++)
{ {
@ -315,19 +361,19 @@ public class Rename
distinctCount = distinctCollection.Count; distinctCount = distinctCollection.Count;
if (toDoCollection.Count != distinctCount) if (toDoCollection.Count != distinctCount)
continue; continue;
if (!string.IsNullOrEmpty(_AppSettings.CopyTo)) verifiedToDoCollection = new();
CopyInstead(toDoCollection); foreach ((FileHolder fileHolder, string to) in toDoCollection)
else
{ {
foreach ((FileHolder fileHolder, string to) in toDoCollection) if (File.Exists(to))
{ continue;
results.Add(fileHolder.NameWithoutExtension); verifiedToDoCollection.Add(new(fileHolder, to));
if (File.Exists(to))
continue;
File.Move(fileHolder.FullName, to);
File.WriteAllText($"{to}.id", $"{to}{Environment.NewLine}{fileHolder.FullName}");
}
} }
if (!verifiedToDoCollection.Any())
continue;
if (string.IsNullOrEmpty(_AppSettings.CopyTo))
results.AddRange(Move(log, toDoCollection));
else
results.AddRange(CopyInstead(log, toDoCollection));
progressBar.Dispose(); progressBar.Dispose();
} }
return results; return results;