Jelajahi Sumber

Working with file extension tranfer

Clemsim 2 tahun lalu
induk
melakukan
063d87252a
3 mengubah file dengan 49 tambahan dan 10 penghapusan
  1. 5 0
      .vscode/settings.json
  2. 44 10
      src/main.rs
  3. 0 0
      src/utils/file_extension.rs

+ 5 - 0
.vscode/settings.json

@@ -0,0 +1,5 @@
+{
+    "rust-analyzer.linkedProjects": [
+        ".\\Cargo.toml"
+    ]
+}

+ 44 - 10
src/main.rs

@@ -1,18 +1,52 @@
-use std::fs;
-use std::path::Path;
+use std::{fs};
+use std::path::{Path};
+
+
+const MAX_DEPTH: usize = 6;
 
 fn main() {
-    let dir_path = "E:\\Users\\cleme\\Downloads";
-    recursive(dir_path)
+    println!("init");
+    search_directory_recursive("E:\\Users\\cleme\\Documents\\Development", 0)
 }
-fn recursive<P: AsRef<Path>>(path: P) {
-    if let Ok(entrie) = fs::read_dir(path) {
-        for entry in entrie{
-            if let Ok(entry_ok) = entry {
-                if let Ok(file_type) = entry_ok.file_type() {
-                    println!("{:?} : {:?}",entry_ok.path(), file_type.is_dir())
+fn search_directory_recursive(directory_path: impl AsRef<Path>, depth: usize) {
+    let directory_path = directory_path.as_ref();
+    println!("{:?}", directory_path);
+    
+    if let Ok(entries) = fs::read_dir(directory_path) {
+        for entry in entries {
+            if let Ok(entry) = entry {
+                let file_path = entry.path();
+                if file_path.is_file() {
+                    if let Some(extension) = file_extension(file_path){
+                        println!("Filetype: {}", extension)
+                    }
+                } else if file_path.is_dir() && depth < MAX_DEPTH && verify_path(&file_path) {
+                    search_directory_recursive(file_path, depth + 1);
                 }
             }
         }
     }
 }
+
+fn file_extension(p: impl AsRef<Path>) -> Option<String> {
+    let p_in = p.as_ref();
+    if let Ok(metadata) = fs::metadata(p_in) {
+        if metadata.is_file() {
+            if let Some(extension) = p_in
+                .extension()
+                .and_then(|ext| ext.to_str())
+            {
+                return Some(extension.to_string());
+            }
+        }
+    } else {
+        println!("Bad path: {:?}", p_in);
+    }
+    None
+}
+
+
+fn verify_path(p: &Path) -> bool {
+    let path_string = p.to_string_lossy().to_lowercase();
+    !(path_string.contains("node_modules")) && !(path_string.contains("target")) && !(path_string.contains(".git")) && !(path_string.contains("venv"))
+}

+ 0 - 0
src/utils/file_extension.rs