70 lines
2.0 KiB
C#
70 lines
2.0 KiB
C#
using LCU.Library.Internal.FileOperationsModels.Models.NameSettings;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace LCU.Library.Internal.FileOperationsModels.Shared
|
|
{
|
|
public static class RegexNameHandler
|
|
{
|
|
public static ICollection<string> BuildFileList(string path, string name, DynamicNameSetting nameSetting)
|
|
{
|
|
var fileList = new List<string>();
|
|
|
|
if (nameSetting == null ||
|
|
nameSetting.RegexSettings == null ||
|
|
!nameSetting.RegexSettings.Any())
|
|
{
|
|
fileList.Add(name);
|
|
return fileList;
|
|
}
|
|
|
|
DirectoryInfo localDirectory = new DirectoryInfo(path);
|
|
var regexName = BuildRegexName(name, nameSetting);
|
|
fileList = localDirectory.GetFiles().Where(file => regexName.IsMatch(file.Name)).Select(file => file.Name).ToList();
|
|
|
|
return fileList;
|
|
}
|
|
|
|
public static ICollection<string> BuildDirectoryList(string path, string name, DynamicNameSetting nameSetting)
|
|
{
|
|
var directoriesList = new List<string>();
|
|
|
|
if (nameSetting == null ||
|
|
nameSetting.RegexSettings == null ||
|
|
string.IsNullOrWhiteSpace(name))
|
|
{
|
|
directoriesList.Add(name);
|
|
return directoriesList;
|
|
}
|
|
|
|
DirectoryInfo localDirectory = new DirectoryInfo(path);
|
|
var regexName = BuildRegexName(name, nameSetting);
|
|
directoriesList = localDirectory.GetDirectories().Where(directory => regexName.IsMatch(directory.Name)).Select(directory => directory.Name).ToList();
|
|
|
|
return directoriesList;
|
|
}
|
|
|
|
public static Regex BuildRegexName(string name, DynamicNameSetting nameSettings)
|
|
{
|
|
foreach (var formatSetting in nameSettings.RegexSettings)
|
|
{
|
|
name = ReplaceTagWithRegex(name, formatSetting.Tag, formatSetting.Regex);
|
|
}
|
|
|
|
return new Regex(name);
|
|
}
|
|
|
|
public static string ReplaceTagWithRegex(string name, string tag, string regex)
|
|
{
|
|
if (string.IsNullOrEmpty(tag) || string.IsNullOrWhiteSpace(regex))
|
|
{
|
|
return name.Trim();
|
|
}
|
|
|
|
return name.Replace(tag, regex);
|
|
}
|
|
}
|
|
}
|