Function filetools::list_nested_files_with_filter
source · pub async fn list_nested_files_with_filter<P: AsRef<Path> + Send>(
path: P,
pattern: FtFilter
) -> Result<Vec<PathBuf>>
Expand description
Lists files in a folder (including ALL subdirectories) matching a filter pattern.
This pattern can be a String
, PathBuf
, or a regex::Regex
pattern.
Use responsibly.
§Sync
For the sync
version, see crate::sync::list_nested_files_with_filter
§Errors
This function will return an error in the following situations:
- The given path is a file and not a directory
- The given path does not exist
§Example
use regex::Regex;
use std::path::PathBuf;
use filetools::{list_nested_files_with_filter, FtFilter};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let root = "some/path/containing/nested/folders/with/files";
// List all files containing the phrase `log`
let mut filter = FtFilter::Raw("log".to_string());
let mut results = list_nested_files_with_filter(&root, filter).await?;
// List all files containing the path segment `files/test`
filter = FtFilter::Path(PathBuf::from("files/test"));
results = list_nested_files_with_filter(&root, filter).await?;
// List all files ending with `.rs`
let re = Regex::new(r"(.*)\.rs").expect("unable to create regex");
filter = FtFilter::Regex(re);
results = list_nested_files_with_filter(&root, filter).await?;
Ok(())
}