Including .json and .html after creating .iso (Day-Helper-2024-12-17)
This commit is contained in:
8
.vscode/launch.json
vendored
8
.vscode/launch.json
vendored
@ -11,6 +11,14 @@
|
|||||||
"preLaunchTask": "build",
|
"preLaunchTask": "build",
|
||||||
"program": "${workspaceFolder}/bin/Debug/net8.0/win-x64/File-Folder-Helper.dll",
|
"program": "${workspaceFolder}/bin/Debug/net8.0/win-x64/File-Folder-Helper.dll",
|
||||||
"args": [
|
"args": [
|
||||||
|
"s",
|
||||||
|
"X",
|
||||||
|
"D:/5-Other-Small",
|
||||||
|
"Day-Helper-2024-12-17",
|
||||||
|
".job.json",
|
||||||
|
"thumbs.db~sync.ffs_db~verify.json~.html",
|
||||||
|
"D:/0-ISO-A",
|
||||||
|
"D:/5-Other-Small/Proxmox/Snap2HTML/Snap2HTML.exe",
|
||||||
"s",
|
"s",
|
||||||
"M",
|
"M",
|
||||||
"D:/5-Other-Small/Notes/EC-Documentation",
|
"D:/5-Other-Small/Notes/EC-Documentation",
|
||||||
|
@ -24,9 +24,12 @@ internal static partial class Helper20241217
|
|||||||
long Length,
|
long Length,
|
||||||
string RelativePath);
|
string RelativePath);
|
||||||
|
|
||||||
private record Record(string Directory,
|
private record Record(string DestinationDirectory,
|
||||||
|
string DirectoryName,
|
||||||
Job Job,
|
Job Job,
|
||||||
string Path);
|
string Path,
|
||||||
|
string Snap2HyperTextMarkupLanguage,
|
||||||
|
string SourceDirectory);
|
||||||
|
|
||||||
private record Job(string? AlternatePath,
|
private record Job(string? AlternatePath,
|
||||||
string Directory,
|
string Directory,
|
||||||
@ -49,14 +52,16 @@ internal static partial class Helper20241217
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static ReadOnlyCollection<File> GetFiles(string searchPattern, string[] ignoreFileNames, Record record) =>
|
||||||
|
GetFiles(record.SourceDirectory, searchPattern, ignoreFileNames);
|
||||||
|
|
||||||
internal static void Backup(ILogger<Worker> logger, List<string> args)
|
internal static void Backup(ILogger<Worker> logger, List<string> args)
|
||||||
{
|
{
|
||||||
Job jobNew;
|
Job jobNew;
|
||||||
string path;
|
string path;
|
||||||
string? json;
|
string? json;
|
||||||
string asidePath;
|
string fileName;
|
||||||
bool areTheyTheSame;
|
bool areTheyTheSame;
|
||||||
string directoryName;
|
|
||||||
IEnumerable<Record> records;
|
IEnumerable<Record> records;
|
||||||
logger.LogInformation(args[0]);
|
logger.LogInformation(args[0]);
|
||||||
logger.LogInformation(args[1]);
|
logger.LogInformation(args[1]);
|
||||||
@ -69,7 +74,7 @@ internal static partial class Helper20241217
|
|||||||
string[] ignoreFileNames = args[3].Split('~');
|
string[] ignoreFileNames = args[3].Split('~');
|
||||||
string destination = Path.GetFullPath(args[4]);
|
string destination = Path.GetFullPath(args[4]);
|
||||||
string sourceDirectory = Path.GetFullPath(args[0]);
|
string sourceDirectory = Path.GetFullPath(args[0]);
|
||||||
char destinationDriveLetter = destination.Split(':')[0][0];
|
string? snap2HyperTextMarkupLanguage = args.Count < 6 ? null : Path.GetFullPath(args[5]);
|
||||||
logger.LogInformation("Searching <{sourceDirectory}> with search pattern {searchPattern}", args[0], searchPattern);
|
logger.LogInformation("Searching <{sourceDirectory}> with search pattern {searchPattern}", args[0], searchPattern);
|
||||||
if (Debugger.IsAttached)
|
if (Debugger.IsAttached)
|
||||||
Verify(searchPattern, ignoreFileNames);
|
Verify(searchPattern, ignoreFileNames);
|
||||||
@ -85,12 +90,14 @@ internal static partial class Helper20241217
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
records = GetRecords(sourceDirectory, searchPatternFiles);
|
records = GetRecords(sourceDirectory, destination, searchPatternFiles);
|
||||||
foreach (Record record in records)
|
foreach (Record record in records)
|
||||||
{
|
{
|
||||||
if (record.Job is null || string.IsNullOrEmpty(record.Job.Extension))
|
if (record.Job is null || string.IsNullOrEmpty(record.Job.Extension))
|
||||||
continue;
|
continue;
|
||||||
logger.LogInformation("Searching <{directory}>", record.Directory);
|
logger.LogInformation("Searching <{directory}>", record.SourceDirectory);
|
||||||
|
if (snap2HyperTextMarkupLanguage is not null && System.IO.File.Exists(snap2HyperTextMarkupLanguage))
|
||||||
|
WriteSnap2HyperTextMarkupLanguage(logger, snap2HyperTextMarkupLanguage, record);
|
||||||
files = GetFiles(searchPattern, ignoreFileNames, record);
|
files = GetFiles(searchPattern, ignoreFileNames, record);
|
||||||
jobNew = GetJob(searchPattern, ignoreFileNames, record, files);
|
jobNew = GetJob(searchPattern, ignoreFileNames, record, files);
|
||||||
json = JsonSerializer.Serialize(jobNew, JobSourceGenerationContext.Default.Job);
|
json = JsonSerializer.Serialize(jobNew, JobSourceGenerationContext.Default.Job);
|
||||||
@ -100,16 +107,13 @@ internal static partial class Helper20241217
|
|||||||
WriteAllText(record.Path, json);
|
WriteAllText(record.Path, json);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
directoryName = Path.GetFileName(record.Directory);
|
fileName = $"{record.DirectoryName}-{DateTime.Now:yyyy-MM-dd-HH-mm-ss-fff}{record.Job.Extension}";
|
||||||
asidePath = Path.Combine(record.Directory, $"{directoryName}-{DateTime.Now:yyyy-MM-dd-HH-mm-ss-fff}{record.Job.Extension}");
|
path = Path.Combine(record.DestinationDirectory, fileName);
|
||||||
path = $"{destinationDriveLetter}{asidePath[1..]}";
|
|
||||||
logger.LogInformation("Writing <{path}> extension", path);
|
logger.LogInformation("Writing <{path}> extension", path);
|
||||||
WritePassedExtension(record, files, directoryName, path);
|
WritePassedExtension(record, files, record.DirectoryName, path);
|
||||||
logger.LogInformation("Wrote <{path}> extension", path);
|
logger.LogInformation("Wrote <{path}> extension", path);
|
||||||
MovePassedExtension(destination, path);
|
|
||||||
logger.LogInformation("Moved <{path}> extension", path);
|
logger.LogInformation("Moved <{path}> extension", path);
|
||||||
WriteAllText(record.Path, json);
|
WriteAllText(record, json, path);
|
||||||
Helpers.HelperDeleteEmptyDirectories.DeleteEmptyDirectories(logger, $"{destinationDriveLetter}{record.Directory[1..]}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -159,23 +163,28 @@ internal static partial class Helper20241217
|
|||||||
return results.AsReadOnly();
|
return results.AsReadOnly();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IEnumerable<Record> GetRecords(string directory, IEnumerable<string> files)
|
private static IEnumerable<Record> GetRecords(string directory, string destination, IEnumerable<string> files)
|
||||||
{
|
{
|
||||||
Job? job;
|
Job? job;
|
||||||
string json;
|
string json;
|
||||||
Record record;
|
Record record;
|
||||||
string fileName;
|
string fileName;
|
||||||
string directoryName;
|
string directoryName;
|
||||||
|
string sourceDirectory;
|
||||||
|
string destinationDirectory;
|
||||||
|
string snap2HyperTextMarkupLanguage;
|
||||||
foreach (string file in files)
|
foreach (string file in files)
|
||||||
{
|
{
|
||||||
fileName = Path.GetFileName(file);
|
fileName = Path.GetFileName(file);
|
||||||
directoryName = Path.GetDirectoryName(file) ?? throw new Exception();
|
sourceDirectory = Path.GetDirectoryName(file) ?? throw new Exception();
|
||||||
|
directoryName = Path.GetFileName(sourceDirectory);
|
||||||
if (!fileName.StartsWith('.'))
|
if (!fileName.StartsWith('.'))
|
||||||
{
|
{
|
||||||
System.IO.File.Move(file, Path.Combine(directoryName, $".{fileName}"));
|
System.IO.File.Move(file, Path.Combine(sourceDirectory, $".{fileName}"));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
json = System.IO.File.ReadAllText(file);
|
json = System.IO.File.ReadAllText(file);
|
||||||
|
snap2HyperTextMarkupLanguage = Path.Combine(sourceDirectory, ".html");
|
||||||
if (string.IsNullOrEmpty(json) || json is "{}" or "[]")
|
if (string.IsNullOrEmpty(json) || json is "{}" or "[]")
|
||||||
job = null;
|
job = null;
|
||||||
else
|
else
|
||||||
@ -188,11 +197,36 @@ internal static partial class Helper20241217
|
|||||||
FilesTotalLength: 0,
|
FilesTotalLength: 0,
|
||||||
Keep: 3,
|
Keep: 3,
|
||||||
Targets: []);
|
Targets: []);
|
||||||
record = new(Directory: directoryName, Job: job, Path: file);
|
destinationDirectory = $"{destination}{sourceDirectory[2..]}";
|
||||||
|
if (!Directory.Exists(destinationDirectory))
|
||||||
|
_ = Directory.CreateDirectory(destinationDirectory);
|
||||||
|
record = new(DestinationDirectory: destinationDirectory,
|
||||||
|
DirectoryName: directoryName,
|
||||||
|
Job: job,
|
||||||
|
Path: file,
|
||||||
|
Snap2HyperTextMarkupLanguage: snap2HyperTextMarkupLanguage,
|
||||||
|
SourceDirectory: sourceDirectory);
|
||||||
yield return record;
|
yield return record;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void WriteSnap2HyperTextMarkupLanguage(ILogger<Worker> logger, string snap2HyperTextMarkupLanguage, Record record)
|
||||||
|
{
|
||||||
|
string title = Path.GetFileName(record.SourceDirectory);
|
||||||
|
ProcessStartInfo processStartInfo = new()
|
||||||
|
{
|
||||||
|
Arguments = $"-path:\"{record.SourceDirectory}\" -outfile:\"{record.Snap2HyperTextMarkupLanguage}\" -title:\"{title}\" -hidden -silent",
|
||||||
|
FileName = snap2HyperTextMarkupLanguage,
|
||||||
|
RedirectStandardError = true,
|
||||||
|
RedirectStandardInput = true,
|
||||||
|
RedirectStandardOutput = true,
|
||||||
|
WorkingDirectory = record.SourceDirectory
|
||||||
|
};
|
||||||
|
Process? process = Process.Start(processStartInfo) ?? throw new Exception("Process should not be null.");
|
||||||
|
process.WaitForExit();
|
||||||
|
logger.LogInformation($"Snap2HyperTextMarkupLanguage: {process.StandardOutput.ReadToEnd()}{Environment.NewLine}{process.StandardError.ReadToEnd()}{Environment.NewLine}{process.ExitCode}");
|
||||||
|
}
|
||||||
|
|
||||||
private static ReadOnlyCollection<File> GetFiles(string directory, string searchPattern, string[] ignoreFileNames)
|
private static ReadOnlyCollection<File> GetFiles(string directory, string searchPattern, string[] ignoreFileNames)
|
||||||
{
|
{
|
||||||
List<File> results = [];
|
List<File> results = [];
|
||||||
@ -217,16 +251,13 @@ internal static partial class Helper20241217
|
|||||||
return results.AsReadOnly();
|
return results.AsReadOnly();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ReadOnlyCollection<File> GetFiles(string searchPattern, string[] ignoreFileNames, Record record) =>
|
|
||||||
GetFiles(record.Directory, searchPattern, ignoreFileNames);
|
|
||||||
|
|
||||||
private static Job GetJob(string searchPattern, string[] ignoreFileNames, Record record, ReadOnlyCollection<File> files)
|
private static Job GetJob(string searchPattern, string[] ignoreFileNames, Record record, ReadOnlyCollection<File> files)
|
||||||
{
|
{
|
||||||
Job result;
|
Job result;
|
||||||
ReadOnlyCollection<File> collection = GetFilteredFiles(searchPattern, ignoreFileNames, files);
|
ReadOnlyCollection<File> collection = GetFilteredFiles(searchPattern, ignoreFileNames, files);
|
||||||
double filesTotalLengthNew = collection.Select(l => l.Length).Sum();
|
double filesTotalLengthNew = collection.Select(l => l.Length).Sum();
|
||||||
result = new(AlternatePath: record.Job.AlternatePath,
|
result = new(AlternatePath: record.Job.AlternatePath,
|
||||||
Directory: record.Directory,
|
Directory: record.SourceDirectory,
|
||||||
Extension: record.Job.Extension,
|
Extension: record.Job.Extension,
|
||||||
Files: collection.ToArray(),
|
Files: collection.ToArray(),
|
||||||
FilesCount: collection.Count,
|
FilesCount: collection.Count,
|
||||||
@ -245,7 +276,7 @@ internal static partial class Helper20241217
|
|||||||
if (filesCountNew != filesCountOld)
|
if (filesCountNew != filesCountOld)
|
||||||
{
|
{
|
||||||
result = false;
|
result = false;
|
||||||
logger.LogInformation("<{directory}> file count has changed {filesCountNew} != {filesCountOld}", record.Directory, filesCountNew, filesCountOld);
|
logger.LogInformation("<{directory}> file count has changed {filesCountNew} != {filesCountOld}", record.SourceDirectory, filesCountNew, filesCountOld);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -254,7 +285,7 @@ internal static partial class Helper20241217
|
|||||||
if (filesTotalLengthNew != filesTotalLengthOld)
|
if (filesTotalLengthNew != filesTotalLengthOld)
|
||||||
{
|
{
|
||||||
result = false;
|
result = false;
|
||||||
logger.LogInformation("<{directory}> file length has changed {filesTotalLengthNew} != {filesTotalLengthOld}", record.Directory, filesTotalLengthNew, filesTotalLengthOld);
|
logger.LogInformation("<{directory}> file length has changed {filesTotalLengthNew} != {filesTotalLengthOld}", record.SourceDirectory, filesTotalLengthNew, filesTotalLengthOld);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -270,7 +301,7 @@ internal static partial class Helper20241217
|
|||||||
WriteAllText(Path.Combine(Environment.CurrentDirectory, ".vscode", "helper", "old.json"), jsonOld);
|
WriteAllText(Path.Combine(Environment.CurrentDirectory, ".vscode", "helper", "old.json"), jsonOld);
|
||||||
WriteAllText(Path.Combine(Environment.CurrentDirectory, ".vscode", "helper", "new.json"), jsonNew);
|
WriteAllText(Path.Combine(Environment.CurrentDirectory, ".vscode", "helper", "new.json"), jsonNew);
|
||||||
}
|
}
|
||||||
logger.LogInformation("<{directory}> file serialized are different {filesTotalLengthNew} != {filesTotalLengthOld}", record.Directory, filesTotalLengthNew, filesTotalLengthOld);
|
logger.LogInformation("<{directory}> file serialized are different {filesTotalLengthNew} != {filesTotalLengthOld}", record.SourceDirectory, filesTotalLengthNew, filesTotalLengthOld);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -294,33 +325,16 @@ internal static partial class Helper20241217
|
|||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void MovePassedExtension(string destination, string path)
|
|
||||||
{
|
|
||||||
string checkPath = $"{destination}{path[2..]}";
|
|
||||||
string checkDirectory = Path.GetDirectoryName(checkPath) ?? throw new Exception();
|
|
||||||
if (!Directory.Exists(checkDirectory))
|
|
||||||
_ = Directory.CreateDirectory(checkDirectory);
|
|
||||||
if (System.IO.File.Exists(checkPath))
|
|
||||||
throw new NotImplementedException($"<{checkPath}> already exists!");
|
|
||||||
System.IO.File.Move(path, checkPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void WriteISO(Record record, ReadOnlyCollection<File> files, string path, string directoryName)
|
private static void WriteISO(Record record, ReadOnlyCollection<File> files, string path, string directoryName)
|
||||||
{
|
{
|
||||||
string checkDirectory = Path.GetDirectoryName(path) ?? throw new Exception();
|
|
||||||
if (!Directory.Exists(checkDirectory))
|
|
||||||
_ = Directory.CreateDirectory(checkDirectory);
|
|
||||||
CDBuilder builder = new() { UseJoliet = true, VolumeIdentifier = directoryName.Length < 25 ? directoryName : directoryName[..25] };
|
CDBuilder builder = new() { UseJoliet = true, VolumeIdentifier = directoryName.Length < 25 ? directoryName : directoryName[..25] };
|
||||||
foreach (File file in files)
|
foreach (File file in files)
|
||||||
_ = builder.AddFile(file.RelativePath, Path.Combine(record.Directory, file.RelativePath));
|
_ = builder.AddFile(file.RelativePath, Path.Combine(record.SourceDirectory, file.RelativePath));
|
||||||
builder.Build(path);
|
builder.Build(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteZIP(Record record, ReadOnlyCollection<File> files, string path)
|
private static void WriteZIP(Record record, ReadOnlyCollection<File> files, string path)
|
||||||
{
|
{
|
||||||
string checkDirectory = Path.GetDirectoryName(path) ?? throw new Exception();
|
|
||||||
if (!Directory.Exists(checkDirectory))
|
|
||||||
_ = Directory.CreateDirectory(checkDirectory);
|
|
||||||
using ZipArchive zip = ZipFile.Open(path, ZipArchiveMode.Create);
|
using ZipArchive zip = ZipFile.Open(path, ZipArchiveMode.Create);
|
||||||
string directoryEntry;
|
string directoryEntry;
|
||||||
List<string> directoryEntries = [];
|
List<string> directoryEntries = [];
|
||||||
@ -333,7 +347,16 @@ internal static partial class Helper20241217
|
|||||||
_ = zip.CreateEntry(file.RelativePath);
|
_ = zip.CreateEntry(file.RelativePath);
|
||||||
}
|
}
|
||||||
foreach (File file in files)
|
foreach (File file in files)
|
||||||
_ = zip.CreateEntryFromFile(Path.Combine(record.Directory, file.RelativePath), file.RelativePath);
|
_ = zip.CreateEntryFromFile(Path.Combine(record.SourceDirectory, file.RelativePath), file.RelativePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void WriteAllText(Record record, string text, string path)
|
||||||
|
{
|
||||||
|
WriteAllText(record.Path, text);
|
||||||
|
System.IO.File.Copy(record.Path, $"{path}.json");
|
||||||
|
string checkFile = Path.Combine(record.SourceDirectory, ".html");
|
||||||
|
if (System.IO.File.Exists(checkFile))
|
||||||
|
System.IO.File.Copy(checkFile, $"{path}.html");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user