diff --git a/Base16384.Net.csproj b/Base16384.Net.csproj index 7f4a039..d8f8d2d 100644 --- a/Base16384.Net.csproj +++ b/Base16384.Net.csproj @@ -11,8 +11,9 @@ - - + + + \ No newline at end of file diff --git a/Helpers.cs b/Helpers.cs index 96be52d..86557a7 100644 --- a/Helpers.cs +++ b/Helpers.cs @@ -1,5 +1,4 @@ using System.Diagnostics.CodeAnalysis; -using System.Globalization; namespace ConsoleHelpers; diff --git a/Program.cs b/Program.cs index eaae04a..610ffb2 100644 --- a/Program.cs +++ b/Program.cs @@ -8,6 +8,23 @@ 4. 编解码失败 5. 无法创建输出文件夹(可能是输出文件夹是一个已存在的文件) */ +if (args is ["debug", string]) { + var lower = args[1].ToLower(new CultureInfo("en-US", false)); + if (lower == "enable") { + Environment.SetEnvironmentVariable("Base16384_Net_Debug", "true"); + Console.WriteLine("Debug mode is enabled."); + return 0; + } + if (lower == "disable") { + Environment.SetEnvironmentVariable("Base16384_Net_Debug", null); + Console.WriteLine("Debug mode is disabled."); + return 0; + } + + Console.WriteLine("Usage: Base16384.Net.exe <\"e\" | \"d\"> [out | \"-\"]"); + return 1; +} + if (args.Length is not 2 and not 3 || args is not ["e", ..] and not ["d", ..]) { Console.WriteLine("Usage: Base16384.Net.exe <\"e\" | \"d\"> [out | \"-\"]"); @@ -93,12 +110,12 @@ 5. 无法创建输出文件夹(可能是输出文件夹是一个已存在的 output = new(Path.Combine(outputDirectoryInfo.FullName, input.Name)); if (output.Exists) { - Console.WriteLine($"{input.Name} -> {output.Name} ... The output file in the directory {outputDirectoryInfo.Name} already exists."); + Console.WriteLine($"{input.Name} -> {output.Name} ... The output file in the directory \"{outputDirectoryInfo.Name}\" already exists."); continue; } if (!input.Exists) { - Console.WriteLine($"{input.Name} -> Source file not found."); + Console.WriteLine($"{input.Name} -> {output.Name} ... Source file not found."); continue; } @@ -131,20 +148,38 @@ 5. 无法创建输出文件夹(可能是输出文件夹是一个已存在的 } FileInfo output; - string relativePath; + DirectoryInfo outputParentDirectoryInfo; + string relativePath, relativeDirectory; // foreach files in the list foreach (var input in inputDirectoryInfo.EnumerateFiles("*", SearchOption.AllDirectories)) { relativePath = Path.GetRelativePath(inputDirectoryInfo.FullName, input.FullName); output = new(Path.Combine(outputDirectoryInfo.FullName, relativePath)); + relativeDirectory = Path.GetDirectoryName(relativePath)!; + relativeDirectory = string.IsNullOrWhiteSpace(relativeDirectory) ? "." : relativeDirectory; + + outputParentDirectoryInfo = output.Directory!; + + if (!outputParentDirectoryInfo.Exists) { + if (File.Exists(outputParentDirectoryInfo.FullName)) { + Console.WriteLine($"{input.Name} -> {output.Name} ... The output directory \"{relativeDirectory}\" is an existing file."); + } + + try { + outputParentDirectoryInfo.Create(); + } catch (Exception e) { + Helpers.PrintException($"{input.Name} -> {output.Name} ... Failed to create output directory \"{outputParentDirectoryInfo.FullName}\".", e, 5); + continue; + } + } if (output.Exists) { - Console.WriteLine($"{input.Name} -> {output.Name} ... The output file in the directory {Path.GetDirectoryName(relativePath)} already exists."); + Console.WriteLine($"{input.Name} -> {output.Name} ... The output file in the directory \"{relativeDirectory}\" already exists."); continue; } if (!input.Exists) { - Console.WriteLine($"{input.Name} -> Source file not found."); + Console.WriteLine($"{input.Name} -> {output.Name} ... Source file not found."); continue; }