Last active
December 9, 2022 01:02
-
-
Save mstfydmr/c90db8fcefeb4eb89696e6ccb5b28685 to your computer and use it in GitHub Desktop.
Go Lang - Scan Folder Recursive. Find all files and folders.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"path/filepath" | |
"strings" | |
"log" | |
) | |
func scan_recursive(dir_path string, ignore []string) ([]string, []string) { | |
folders := []string{} | |
files := []string{} | |
// Scan | |
filepath.Walk(dir_path, func(path string, f os.FileInfo, err error) error { | |
_continue := false | |
// Loop : Ignore Files & Folders | |
for _, i := range ignore { | |
// If ignored path | |
if strings.Index(path, i) != -1 { | |
// Continue | |
_continue = true | |
} | |
} | |
if _continue == false { | |
f, err = os.Stat(path) | |
// If no error | |
if err != nil { | |
log.Fatal(err) | |
} | |
// File & Folder Mode | |
f_mode := f.Mode() | |
// Is folder | |
if f_mode.IsDir() { | |
// Append to Folders Array | |
folders = append(folders, path) | |
// Is file | |
} else if f_mode.IsRegular(){ | |
// Append to Files Array | |
files = append(files, path) | |
} | |
} | |
return nil | |
}) | |
return folders, files | |
} | |
func main() { | |
folders, files := scan_recursive("/Users/mustafa/vhosts", []string{".git", "/.git", "/.git/", ".gitignore", ".DS_Store", ".idea", "/.idea/", "/.idea"}) | |
// Files | |
for _, i := range files { | |
fmt.Println(i) | |
} | |
// Folders | |
for _, i := range folders { | |
fmt.Println(i) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment