Files
SabreTools/SabreTools.IO/PathTool.cs

175 lines
7.2 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.IO;
using System.Linq;
2020-12-08 14:53:49 -08:00
using NaturalSort;
2020-12-07 15:08:57 -08:00
namespace SabreTools.IO
{
/// <summary>
2020-12-10 22:16:53 -08:00
/// Methods around path operations
/// </summary>
2020-12-10 22:16:53 -08:00
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>
2020-07-26 23:39:33 -07:00
/// <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>
2020-07-26 23:39:33 -07:00
public static List<ParentablePath> GetDirectoriesOnly(List<string> inputs, bool appendparent = false)
{
List<ParentablePath> outputs = new();
2020-08-08 21:53:34 -07:00
for (int i = 0; i < inputs.Count; i++)
{
2020-08-08 21:53:34 -07:00
string input = inputs[i];
2020-08-17 14:10:53 -07:00
// If we have a null or empty path
if (string.IsNullOrEmpty(input))
continue;
2020-08-08 21:53:34 -07:00
// If we have a wildcard
string pattern = "*";
if (input.Contains('*') || input.Contains('?'))
2020-08-08 21:53:34 -07:00
{
pattern = Path.GetFileName(input);
input = input[..^pattern.Length];
2020-08-08 21:53:34 -07:00
}
2020-08-17 14:10:53 -07:00
// Get the parent path in case of appending
2020-12-08 13:23:59 -08:00
string parentPath = Path.GetFullPath(input);
2020-08-17 14:10:53 -07:00
if (Directory.Exists(input))
{
2020-08-08 21:53:34 -07:00
List<string> directories = GetDirectoriesOrdered(input, pattern);
foreach (string dir in directories)
{
2020-12-08 13:23:59 -08:00
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>
2020-08-08 21:53:34 -07:00
/// <param name="pattern">Optional pattern to search for directory names</param>
/// <returns>List with all new files</returns>
2020-08-08 21:53:34 -07:00
private static List<string> GetDirectoriesOrdered(string dir, string pattern = "*")
{
2020-08-08 21:53:34 -07:00
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>
2020-08-08 21:53:34 -07:00
/// <param name="pattern">Optional pattern to search for directory names</param>
/// <returns>List with all new files</returns>
2020-08-08 21:53:34 -07:00
private static List<string> GetDirectoriesOrderedHelper(string dir, List<string> infiles, string pattern)
{
// Take care of the files in the top directory
2020-08-08 21:53:34 -07:00
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)
{
2020-08-08 21:53:34 -07:00
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>
2020-07-26 23:39:33 -07:00
/// <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>
2020-07-26 23:39:33 -07:00
public static List<ParentablePath> GetFilesOnly(List<string> inputs, bool appendparent = false)
{
List<ParentablePath> outputs = new();
2020-08-08 21:53:34 -07:00
for (int i = 0; i < inputs.Count; i++)
{
string input = inputs[i].Trim('"');
2020-08-08 21:53:34 -07:00
2020-08-17 14:10:53 -07:00
// If we have a null or empty path
if (string.IsNullOrEmpty(input))
continue;
2020-08-08 21:53:34 -07:00
// If we have a wildcard
string pattern = "*";
if (input.Contains('*') || input.Contains('?'))
2020-08-08 21:53:34 -07:00
{
pattern = Path.GetFileName(input);
input = input[..^pattern.Length];
2020-08-08 21:53:34 -07:00
}
2020-07-26 23:39:33 -07:00
// Get the parent path in case of appending
2020-12-08 13:23:59 -08:00
string parentPath = Path.GetFullPath(input);
if (Directory.Exists(input))
{
2020-08-08 21:53:34 -07:00
List<string> files = GetFilesOrdered(input, pattern);
foreach (string file in files)
{
2020-12-08 13:23:59 -08:00
outputs.Add(new ParentablePath(Path.GetFullPath(file), appendparent ? parentPath : string.Empty));
}
}
else if (File.Exists(input))
{
2020-12-08 13:23:59 -08:00
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>
2020-08-08 21:53:34 -07:00
/// <param name="pattern">Optional pattern to search for directory names</param>
/// <returns>List with all new files</returns>
2020-08-08 21:53:34 -07:00
public static List<string> GetFilesOrdered(string dir, string pattern = "*")
{
2020-08-08 21:53:34 -07:00
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>
2020-08-08 21:53:34 -07:00
/// <param name="pattern">Optional pattern to search for directory names</param>
/// <returns>List with all new files</returns>
2020-08-08 21:53:34 -07:00
private static List<string> GetFilesOrderedHelper(string dir, List<string> infiles, string pattern)
{
// Take care of the files in the top directory
2020-08-08 21:53:34 -07:00
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
2020-08-08 21:53:34 -07:00
List<string> subDirs = Directory.EnumerateDirectories(dir, pattern, SearchOption.TopDirectoryOnly).ToList();
subDirs.Sort(new NaturalComparer());
foreach (string subdir in subDirs)
{
2020-08-08 21:53:34 -07:00
infiles = GetFilesOrderedHelper(subdir, infiles, pattern);
}
// Return the new list
return infiles;
}
2020-12-11 22:52:28 -08:00
/// <summary>
/// Get the current runtime directory
/// </summary>
2023-01-20 10:56:39 -08:00
public static string GetRuntimeDirectory() => Directory.GetCurrentDirectory();
}
}