From e0d290306b5a2c5b2f5652a2e10f91fb70c273ca Mon Sep 17 00:00:00 2001 From: Tichau Date: Tue, 27 Oct 2015 19:23:00 +0100 Subject: [PATCH] Added: Create folders when necessary. --- .../ConversionJobs/ConversionJob.cs | 25 ++++++++++++++++- Application/FileConverter/PathHelpers.cs | 27 ++++++++++++++----- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/Application/FileConverter/ConversionJobs/ConversionJob.cs b/Application/FileConverter/ConversionJobs/ConversionJob.cs index 453624a..31d7fdc 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob.cs @@ -5,6 +5,7 @@ namespace FileConverter.ConversionJobs using System; using System.ComponentModel; using System.Runtime.CompilerServices; + using System.Text; public class ConversionJob : INotifyPropertyChanged { @@ -122,7 +123,29 @@ namespace FileConverter.ConversionJobs return; } - // TODO: Check is all output folders exists. + // Check if all output folders exists. + { + StringBuilder path = new StringBuilder(this.OutputFilePath.Length); + foreach (string directory in PathHelpers.GetDirectories(this.OutputFilePath)) + { + path.Append(directory); + path.Append('\\'); + + if (!System.IO.Directory.Exists(path.ToString())) + { + try + { + System.IO.Directory.CreateDirectory(path.ToString()); + } + catch (Exception) + { + this.ConvertionFailed("Invalid output path generated by output file path template."); + Diagnostics.Log(string.Format("Can't create directories for path {0}", this.OutputFilePath)); + return; + } + } + } + } try { diff --git a/Application/FileConverter/PathHelpers.cs b/Application/FileConverter/PathHelpers.cs index d75ba8a..7de417a 100644 --- a/Application/FileConverter/PathHelpers.cs +++ b/Application/FileConverter/PathHelpers.cs @@ -1,17 +1,16 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading.Tasks; +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. namespace FileConverter { + using System.Collections.Generic; + using System.Text.RegularExpressions; + public static class PathHelpers { private static Regex driveLetterRegex = new Regex(@"[a-zA-Z]:\\"); private static Regex pathRegex = new Regex(@"^[a-zA-Z]:\\(?:[^\\/:*?""<>|\r\n]+\\)*[^\.\\/:*?""<>|\r\n][^\\/:*?""<>|\r\n]*$"); private static Regex filenameRegex = new Regex(@"[^\\]*", RegexOptions.RightToLeft); + private static Regex directoryRegex = new Regex(@"(?:([^\\]*)\\)*"); public static bool IsPathDriveLetterValid(string path) { @@ -29,5 +28,21 @@ namespace FileConverter Match filenameMatch = matchCollection.Count > 0 ? matchCollection[0] : null; return filenameMatch?.Groups[0].Value; } + + public static IEnumerable GetDirectories(string path) + { + MatchCollection matchCollection = PathHelpers.directoryRegex.Matches(path); + Match match = matchCollection.Count > 0 ? matchCollection[0] : null; + if (match == null || match.Groups.Count < 2) + { + yield break; + } + + Group matchGroup = match.Groups[1]; + for (int index = 0; index < matchGroup.Captures.Count; index++) + { + yield return matchGroup.Captures[index].Value; + } + } } }