2023-12-12 SplitJsonFile 2023-11-28 Migrated to File-Watcher 2023-12-05 SplitMarkdownFile 2023-11-30 RenameReactorProcessDataStandardFormatFiles 2023-11-22 ProcessDataStandardFormat
99 lines
3.8 KiB
C#
99 lines
3.8 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace File_Folder_Helper.Day;
|
|
|
|
internal static partial class Helper20231205
|
|
{
|
|
|
|
[GeneratedRegex(@"[\\,\/,\:,\*,\?,\"",\<,\>,\|]")]
|
|
private static partial Regex WindowsSafe();
|
|
|
|
private static string? GetStrippedIPV4(string[] segments)
|
|
{
|
|
string? result = null;
|
|
string[] subSegments;
|
|
foreach (string segment in segments)
|
|
{
|
|
subSegments = segment.Split('.');
|
|
if (subSegments.Length != 4)
|
|
continue;
|
|
if (!subSegments.All(l => int.TryParse(l, out _)))
|
|
continue;
|
|
result = segment.Replace(".", string.Empty);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static string? GetStrippedMacAddress(string[] segments)
|
|
{
|
|
string? result = null;
|
|
foreach (string segment in segments)
|
|
{
|
|
if (segment.Length != 17)
|
|
continue;
|
|
if (segment[2] is not ':' or '-' || segment[5] is not ':' or '-' || segment[8] is not ':' or '-' || segment[11] is not ':' or '-' || segment[14] is not ':' or '-')
|
|
continue;
|
|
result = $"{segment[0]}{segment[1]}{segment[3]}{segment[4]}{segment[6]}{segment[7]}{segment[9]}{segment[10]}{segment[12]}{segment[13]}{segment[15]}{segment[16]}".ToLower();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
internal static void SplitMarkdownFile(ILogger<Worker> logger, List<string> args)
|
|
{
|
|
string[] lines;
|
|
string? fileName;
|
|
string[] segments;
|
|
string checkFileName;
|
|
string? strippedIpV4;
|
|
string? strippedMacAddress;
|
|
List<string> collection = [];
|
|
string sourceDirectory = args[0];
|
|
if (!Directory.Exists(sourceDirectory))
|
|
throw new Exception(sourceDirectory);
|
|
string outputDirectory = Path.Combine(sourceDirectory, Path.GetFileNameWithoutExtension(args[2]));
|
|
if (!Directory.Exists(outputDirectory))
|
|
_ = Directory.CreateDirectory(outputDirectory);
|
|
string[] files = Directory.GetFiles(args[0], args[2], SearchOption.TopDirectoryOnly);
|
|
foreach (string file in files)
|
|
{
|
|
fileName = null;
|
|
collection.Clear();
|
|
lines = File.ReadAllLines(file);
|
|
foreach (string line in lines)
|
|
{
|
|
collection.Add(line);
|
|
if (line.Length > 0 && line[0] == '#' && line.StartsWith("## "))
|
|
{
|
|
segments = line.Split(' ');
|
|
strippedIpV4 = GetStrippedIPV4(segments);
|
|
strippedMacAddress = GetStrippedMacAddress(segments);
|
|
if (strippedMacAddress is null && strippedIpV4 is null)
|
|
fileName = $"{WindowsSafe().Replace(line[3..], "-").Trim().ToLower()}.md";
|
|
else if (strippedMacAddress is null)
|
|
{
|
|
fileName = $"ipv4-{strippedIpV4}.md";
|
|
collection.Insert(0, string.Empty);
|
|
collection.Insert(0, $"# {fileName}");
|
|
}
|
|
else
|
|
{
|
|
fileName = $"mac-{strippedMacAddress}.md";
|
|
collection.Insert(0, string.Empty);
|
|
collection.Insert(0, $"# {fileName}");
|
|
}
|
|
}
|
|
if (fileName is null || line != "----")
|
|
continue;
|
|
collection.RemoveAt(collection.Count - 1);
|
|
logger.LogInformation("{fileName} created", fileName);
|
|
checkFileName = Path.Combine(outputDirectory, fileName);
|
|
if (File.Exists(checkFileName))
|
|
File.Delete(checkFileName);
|
|
File.WriteAllLines(checkFileName, collection);
|
|
collection.Clear();
|
|
}
|
|
}
|
|
}
|
|
|
|
} |