file_mon/
lib.rs

1use std::fs;
2use std::path::Path;
3
4/// Count the number of folders in a directory recursively.
5///
6/// # Arguments
7///
8/// * `path` - A reference to a `Path` representing the directory to count folders for.
9///
10/// # Returns
11///
12/// The number of folders found in the directory and its subdirectories.
13///
14/// # Example
15///
16/// ```
17/// use file_mon::count_folders;
18/// use std::path::Path;
19///
20/// let path = Path::new("/path/to/your/directory");
21/// let folder_count = count_folders(path);
22/// println!("Number of folders: {}", folder_count);
23/// ```
24pub fn count_folders(path: &Path) -> u32 {
25    let mut folder_count = 0;
26
27    if let Ok(entries) = fs::read_dir(path) {
28        for entry in entries {
29            if let Ok(entry) = entry {
30                let metadata = entry.metadata().unwrap(); // Unwrap is safe here for simplicity
31
32                if metadata.is_dir() {
33                    folder_count += 1;
34                    folder_count += count_folders(&entry.path()); // Recursively count subfolders
35                }
36            }
37        }
38    }
39
40    folder_count
41}