mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
182 lines
7.4 KiB
C#
182 lines
7.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
using NaturalSort;
|
|
|
|
namespace SabreTools.IO
|
|
{
|
|
/// <summary>
|
|
/// Methods around path operations
|
|
/// </summary>
|
|
public static class PathTool
|
|
{
|
|
/// <summary>
|
|
/// Retrieve a list of just directories from inputs
|
|
/// </summary>
|
|
/// <param name="inputs">List of strings representing directories and files</param>
|
|
/// <param name="appendparent">True if the parent name should be included in the ParentablePath, false otherwise (default)</param>
|
|
/// <returns>List of strings representing just directories from the inputs</returns>
|
|
public static List<ParentablePath> GetDirectoriesOnly(List<string> inputs, bool appendparent = false)
|
|
{
|
|
List<ParentablePath> outputs = new List<ParentablePath>();
|
|
for (int i = 0; i < inputs.Count; i++)
|
|
{
|
|
string input = inputs[i];
|
|
|
|
// If we have a null or empty path
|
|
if (string.IsNullOrEmpty(input))
|
|
continue;
|
|
|
|
// If we have a wildcard
|
|
string pattern = "*";
|
|
if (input.Contains("*") || input.Contains("?"))
|
|
{
|
|
pattern = Path.GetFileName(input);
|
|
input = input.Substring(0, input.Length - pattern.Length);
|
|
}
|
|
|
|
// Get the parent path in case of appending
|
|
string parentPath = Path.GetFullPath(input);
|
|
if (Directory.Exists(input))
|
|
{
|
|
List<string> directories = GetDirectoriesOrdered(input, pattern);
|
|
foreach (string dir in directories)
|
|
{
|
|
outputs.Add(new ParentablePath(Path.GetFullPath(dir), appendparent ? parentPath : string.Empty));
|
|
}
|
|
}
|
|
}
|
|
|
|
return outputs;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve a list of directories from a directory recursively in proper order
|
|
/// </summary>
|
|
/// <param name="dir">Directory to parse</param>
|
|
/// <param name="pattern">Optional pattern to search for directory names</param>
|
|
/// <returns>List with all new files</returns>
|
|
private static List<string> GetDirectoriesOrdered(string dir, string pattern = "*")
|
|
{
|
|
return GetDirectoriesOrderedHelper(dir, new List<string>(), pattern);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve a list of directories from a directory recursively in proper order
|
|
/// </summary>
|
|
/// <param name="dir">Directory to parse</param>
|
|
/// <param name="infiles">List representing existing files</param>
|
|
/// <param name="pattern">Optional pattern to search for directory names</param>
|
|
/// <returns>List with all new files</returns>
|
|
private static List<string> GetDirectoriesOrderedHelper(string dir, List<string> infiles, string pattern)
|
|
{
|
|
// Take care of the files in the top directory
|
|
List<string> toadd = Directory.EnumerateDirectories(dir, pattern, SearchOption.TopDirectoryOnly).ToList();
|
|
toadd.Sort(new NaturalComparer());
|
|
infiles.AddRange(toadd);
|
|
|
|
// Then recurse through and add from the directories
|
|
foreach (string subDir in toadd)
|
|
{
|
|
infiles = GetDirectoriesOrderedHelper(subDir, infiles, pattern);
|
|
}
|
|
|
|
// Return the new list
|
|
return infiles;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve a list of just files from inputs
|
|
/// </summary>
|
|
/// <param name="inputs">List of strings representing directories and files</param>
|
|
/// <param name="appendparent">True if the parent name should be be included in the ParentablePath, false otherwise (default)</param>
|
|
/// <returns>List of strings representing just files from the inputs</returns>
|
|
public static List<ParentablePath> GetFilesOnly(List<string> inputs, bool appendparent = false)
|
|
{
|
|
List<ParentablePath> outputs = new List<ParentablePath>();
|
|
for (int i = 0; i < inputs.Count; i++)
|
|
{
|
|
string input = inputs[i].Trim('"');
|
|
|
|
// If we have a null or empty path
|
|
if (string.IsNullOrEmpty(input))
|
|
continue;
|
|
|
|
// If we have a wildcard
|
|
string pattern = "*";
|
|
if (input.Contains("*") || input.Contains("?"))
|
|
{
|
|
pattern = Path.GetFileName(input);
|
|
input = input.Substring(0, input.Length - pattern.Length);
|
|
}
|
|
|
|
// Get the parent path in case of appending
|
|
string parentPath = Path.GetFullPath(input);
|
|
if (Directory.Exists(input))
|
|
{
|
|
List<string> files = GetFilesOrdered(input, pattern);
|
|
foreach (string file in files)
|
|
{
|
|
outputs.Add(new ParentablePath(Path.GetFullPath(file), appendparent ? parentPath : string.Empty));
|
|
}
|
|
}
|
|
else if (File.Exists(input))
|
|
{
|
|
outputs.Add(new ParentablePath(Path.GetFullPath(input), appendparent ? parentPath : string.Empty));
|
|
}
|
|
}
|
|
|
|
return outputs;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve a list of files from a directory recursively in proper order
|
|
/// </summary>
|
|
/// <param name="dir">Directory to parse</param>
|
|
/// <param name="pattern">Optional pattern to search for directory names</param>
|
|
/// <returns>List with all new files</returns>
|
|
public static List<string> GetFilesOrdered(string dir, string pattern = "*")
|
|
{
|
|
return GetFilesOrderedHelper(dir, new List<string>(), pattern);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve a list of files from a directory recursively in proper order
|
|
/// </summary>
|
|
/// <param name="dir">Directory to parse</param>
|
|
/// <param name="infiles">List representing existing files</param>
|
|
/// <param name="pattern">Optional pattern to search for directory names</param>
|
|
/// <returns>List with all new files</returns>
|
|
private static List<string> GetFilesOrderedHelper(string dir, List<string> infiles, string pattern)
|
|
{
|
|
// Take care of the files in the top directory
|
|
List<string> toadd = Directory.EnumerateFiles(dir, pattern, SearchOption.TopDirectoryOnly).ToList();
|
|
toadd.Sort(new NaturalComparer());
|
|
infiles.AddRange(toadd);
|
|
|
|
// Then recurse through and add from the directories
|
|
List<string> subDirs = Directory.EnumerateDirectories(dir, pattern, SearchOption.TopDirectoryOnly).ToList();
|
|
subDirs.Sort(new NaturalComparer());
|
|
foreach (string subdir in subDirs)
|
|
{
|
|
infiles = GetFilesOrderedHelper(subdir, infiles, pattern);
|
|
}
|
|
|
|
// Return the new list
|
|
return infiles;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the current runtime directory
|
|
/// </summary>
|
|
public static string GetRuntimeDirectory()
|
|
{
|
|
string exeName = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath;
|
|
return Path.GetDirectoryName(exeName);
|
|
}
|
|
}
|
|
}
|