Last active
September 13, 2017 23:08
-
-
Save brydavis/94a5e3c2e47b57ac4b9d to your computer and use it in GitHub Desktop.
recursive folder scans
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" | |
"io/ioutil" | |
"path" | |
"path/filepath" | |
"strings" | |
"sync" | |
) | |
var wg sync.WaitGroup | |
func main() { | |
wg.Add(1) | |
go ScanDir(".", ".xlsx", 0, func(name string) { | |
fmt.Println(name) | |
// file, _ := os.Open(name) | |
// b := make([]byte, 50) | |
// file.Read(b) | |
// fmt.Printf("%s: %s\n", name, string(b)) | |
}) | |
wg.Wait() | |
} | |
func ScanDir(dir, ext string, indent int, fn func(string)) { | |
defer wg.Done() | |
d, err := ioutil.ReadDir(dir) | |
if err != nil { | |
fmt.Println(err) | |
} | |
for _, f := range d { | |
fname := f.Name() | |
if f.IsDir() { | |
// fmt.Printf("%s%s\t%s (DIR)\n", strings.Repeat("\t", indent), dir, fname) | |
wg.Add(1) | |
go ScanDir(path.Join(dir, fname), ext, indent+1, fn) | |
} else { | |
if ext == "" { | |
fmt.Printf("%s%s\n", strings.Repeat("\t", indent), filepath.Join(dir, fname)) | |
} else if path.Ext(fname) == ext { | |
// fmt.Printf("%s%s\n", strings.Repeat("\t", indent), name) | |
name := filepath.Join(dir, fname) | |
go fn(name) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment