IO/FileSystemExtensions.cs
using Sandbox;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace ExtendedBox.IO;

public static class FileSystemExtensions
{

    public static IEnumerable<string> FindDirectory(this BaseFileSystem fileSystem, Regex regex, bool recursive = false) => fileSystem.FindDirectory("/", regex, recursive);
    public static IEnumerable<string> FindDirectory(this BaseFileSystem fileSystem, string folder, Regex regex, bool recursive = false)
    {
        foreach(var directoryName in fileSystem.FindDirectory(folder, "*", recursive))
        {
            if(regex.IsMatch(directoryName))
                yield return directoryName;
        }
    }

    public static IEnumerable<string> FindFile(this BaseFileSystem fileSystem, Regex regex, bool recursive = false) => fileSystem.FindFile("/", regex, recursive);
    public static IEnumerable<string> FindFile(this BaseFileSystem fileSystem, string folder, Regex regex, bool recursive = false)
    {
        foreach(var directoryName in fileSystem.FindFile(folder, "*", recursive))
        {
            if(regex.IsMatch(directoryName))
                yield return directoryName;
        }
    }

    public static bool TryCreateSubSystem(this BaseFileSystem baseFileSystem, string path, out BaseFileSystem fileSystem)
    {
        if(!baseFileSystem.DirectoryExists(path))
        {
            fileSystem = null!;
            return false;
        }

        fileSystem = baseFileSystem.CreateSubSystem(path);
        return true;
    }
}