From 93ffb1d8ac56d51d09c1896bb792326bd7786993 Mon Sep 17 00:00:00 2001 From: SH4KUR Date: Wed, 10 Jan 2024 18:06:58 +0200 Subject: [PATCH] updated with convert folder in target directory --- AudioBitDepthConvert/Program.cs | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/AudioBitDepthConvert/Program.cs b/AudioBitDepthConvert/Program.cs index d6efa55..983f513 100644 --- a/AudioBitDepthConvert/Program.cs +++ b/AudioBitDepthConvert/Program.cs @@ -10,15 +10,15 @@ private static void Main(string[] args) Console.Write("Enter the path of the directory: "); var directoryPath = Console.ReadLine(); - if (Directory.Exists(directoryPath)) + if (!string.IsNullOrEmpty(directoryPath) && Directory.Exists(directoryPath)) { - ConvertFiles(directoryPath); + ConvertFiles(RemoveDashesFromEndPath(directoryPath)); Console.WriteLine("Converting completed."); } else { - Console.WriteLine("Directory not found."); + Console.WriteLine($"Directory '{directoryPath}' not found."); } } @@ -28,11 +28,11 @@ private static void ConvertFiles(string directoryPath) foreach (var filePath in audioFiles) { - ConvertBitDepth(filePath); + ConvertBitDepth(filePath, directoryPath); } } - private static void ConvertBitDepth(string filePath) + private static void ConvertBitDepth(string filePath, string parentDirectoryPath) { using (var reader = new WaveFileReader(filePath)) { @@ -45,7 +45,7 @@ private static void ConvertBitDepth(string filePath) try { - ExecuteSoxUtility(filePath); + ExecuteSoxUtility(filePath, CreateConvertedFilePath(filePath, parentDirectoryPath)); } catch (Exception e) { @@ -54,10 +54,8 @@ private static void ConvertBitDepth(string filePath) } } - private static void ExecuteSoxUtility(string filePath) + private static void ExecuteSoxUtility(string filePath, string convertedFilePath) { - var convertedFilePath = CreateConvertedFilePath(filePath); - var startInfo = new ProcessStartInfo() { RedirectStandardOutput = true, @@ -71,20 +69,27 @@ private static void ExecuteSoxUtility(string filePath) var process = Process.Start(startInfo) ?? throw new NullReferenceException("Process is null"); process.WaitForExit(); - Console.WriteLine($">>> {convertedFilePath}"); + Console.WriteLine($"> {convertedFilePath}"); } - private static string CreateConvertedFilePath(string filePath) + private static string CreateConvertedFilePath(string filePath, string parentDirectoryPath) { - var directoryPath = Path.GetDirectoryName(filePath); - var fileName = Path.GetFileName(filePath); - var outputDirectoryPath = directoryPath + "\\converted"; + var fileDirectoryPath = Path.GetDirectoryName(filePath) ?? throw new NullReferenceException($"Directory path for '{filePath}' is null"); + var outputDirectoryPath = fileDirectoryPath.Replace(parentDirectoryPath, $"{parentDirectoryPath}\\converted"); if (!Directory.Exists(outputDirectoryPath)) { Directory.CreateDirectory(outputDirectoryPath); } + var fileName = Path.GetFileName(filePath); return $"{outputDirectoryPath}\\{fileName}"; } + + private static string RemoveDashesFromEndPath(string directoryPath) + { + return directoryPath.EndsWith("\\") + ? directoryPath.Remove(directoryPath.Length - 1) + : directoryPath; + } } \ No newline at end of file