2020-07-15 09:41:59 -07:00
|
|
|
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
|
2020-07-15 09:41:59 -07:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
2020-12-10 22:16:53 -08:00
|
|
|
/// Methods around path operations
|
2020-07-15 09:41:59 -07:00
|
|
|
/// </summary>
|
2020-12-10 22:16:53 -08:00
|
|
|
public static class PathTool
|
2020-07-15 09:41:59 -07:00
|
|
|
{
|
|
|
|
|
/// <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>
|
2020-07-15 09:41:59 -07:00
|
|
|
/// <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)
|
2020-07-15 09:41:59 -07:00
|
|
|
{
|
2023-04-19 16:39:58 -04:00
|
|
|
List<ParentablePath> outputs = new();
|
2020-08-08 21:53:34 -07:00
|
|
|
for (int i = 0; i < inputs.Count; i++)
|
2020-07-15 09:41:59 -07:00
|
|
|
{
|
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 = "*";
|
2023-04-19 16:39:58 -04:00
|
|
|
if (input.Contains('*') || input.Contains('?'))
|
2020-08-08 21:53:34 -07:00
|
|
|
{
|
|
|
|
|
pattern = Path.GetFileName(input);
|
2023-04-19 16:39:58 -04:00
|
|
|
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);
|
2020-07-15 09:41:59 -07:00
|
|
|
foreach (string dir in directories)
|
|
|
|
|
{
|
2020-12-08 13:23:59 -08:00
|
|
|
outputs.Add(new ParentablePath(Path.GetFullPath(dir), appendparent ? parentPath : string.Empty));
|
2020-07-15 09:41:59 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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>
|
2020-07-15 09:41:59 -07:00
|
|
|
/// <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-07-15 09:41:59 -07:00
|
|
|
{
|
2020-08-08 21:53:34 -07:00
|
|
|
return GetDirectoriesOrderedHelper(dir, new List<string>(), pattern);
|
2020-07-15 09:41:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2020-07-15 09:41:59 -07:00
|
|
|
/// <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)
|
2020-07-15 09:41:59 -07:00
|
|
|
{
|
|
|
|
|
// 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();
|
2020-07-15 09:41:59 -07:00
|
|
|
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);
|
2020-07-15 09:41:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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>
|
2020-07-15 09:41:59 -07:00
|
|
|
/// <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)
|
2020-07-15 09:41:59 -07:00
|
|
|
{
|
2023-04-19 16:39:58 -04:00
|
|
|
List<ParentablePath> outputs = new();
|
2020-08-08 21:53:34 -07:00
|
|
|
for (int i = 0; i < inputs.Count; i++)
|
2020-07-15 09:41:59 -07:00
|
|
|
{
|
2020-08-26 17:11:24 -07:00
|
|
|
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 = "*";
|
2023-04-19 16:39:58 -04:00
|
|
|
if (input.Contains('*') || input.Contains('?'))
|
2020-08-08 21:53:34 -07:00
|
|
|
{
|
|
|
|
|
pattern = Path.GetFileName(input);
|
2023-04-19 16:39:58 -04:00
|
|
|
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);
|
2020-07-15 09:41:59 -07:00
|
|
|
if (Directory.Exists(input))
|
|
|
|
|
{
|
2020-08-08 21:53:34 -07:00
|
|
|
List<string> files = GetFilesOrdered(input, pattern);
|
2020-07-15 09:41:59 -07:00
|
|
|
foreach (string file in files)
|
|
|
|
|
{
|
2020-12-08 13:23:59 -08:00
|
|
|
outputs.Add(new ParentablePath(Path.GetFullPath(file), appendparent ? parentPath : string.Empty));
|
2020-07-15 09:41:59 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (File.Exists(input))
|
|
|
|
|
{
|
2020-12-08 13:23:59 -08:00
|
|
|
outputs.Add(new ParentablePath(Path.GetFullPath(input), appendparent ? parentPath : string.Empty));
|
2020-07-15 09:41:59 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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>
|
2020-07-15 09:41:59 -07:00
|
|
|
/// <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-07-15 09:41:59 -07:00
|
|
|
{
|
2020-08-08 21:53:34 -07:00
|
|
|
return GetFilesOrderedHelper(dir, new List<string>(), pattern);
|
2020-07-15 09:41:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2020-07-15 09:41:59 -07:00
|
|
|
/// <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)
|
2020-07-15 09:41:59 -07:00
|
|
|
{
|
|
|
|
|
// 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();
|
2020-07-15 09:41:59 -07:00
|
|
|
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();
|
2020-07-15 09:41:59 -07:00
|
|
|
subDirs.Sort(new NaturalComparer());
|
|
|
|
|
foreach (string subdir in subDirs)
|
|
|
|
|
{
|
2020-08-08 21:53:34 -07:00
|
|
|
infiles = GetFilesOrderedHelper(subdir, infiles, pattern);
|
2020-07-15 09:41:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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();
|
2020-07-15 09:41:59 -07:00
|
|
|
}
|
|
|
|
|
}
|