Added: Create folders when necessary.

This commit is contained in:
Tichau committed 2015-10-27 19:23:00 +01:00
1 parent 1d66f9fef0
commit e0d290306b
2 files changed
+45 -7

No files matched your search

@@ -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
{
+21 -6
View File
@@ -1,17 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
// <copyright file="PathHelpers.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
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<string> 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;
}
}
}
}