Created
August 14, 2015 21:11
-
-
Save jahands/690c83c60684abd7f32c to your computer and use it in GitHub Desktop.
Go code for recursively getting files
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" | |
"strconv" | |
"sync" | |
"time" | |
) | |
var wg sync.WaitGroup | |
type set struct { | |
path string | |
isDir bool | |
size int64 | |
} | |
var ( | |
fileCount int | |
dirCount int | |
totalSpace int64 | |
) | |
func main() { | |
startingDir := set{"D:/", true, 0} | |
c := make(chan set) | |
wg.Add(2) | |
fmt.Println("D | ", startingDir.path) | |
start := time.Now() | |
go func() { | |
listFiles(startingDir, true, c) | |
listFiles(set{"C:/", true, 0}, true, c) | |
wg.Wait() | |
close(c) | |
}() | |
for s := range c { | |
// fmt.Println(s.path) | |
if s.isDir { | |
dirCount += 1 | |
} else { | |
fileCount += 1 | |
totalSpace += s.size | |
} | |
} | |
wg.Wait() | |
elapsed := time.Since(start) | |
fmt.Println("Time: ", elapsed) | |
fmt.Println("Files: ", fileCount) | |
fmt.Println("Folders: ", dirCount) | |
fmt.Println("Total size: ", totalSpace/1024/1024, "MB") | |
// fmt.Println("Writing to file...") | |
// err := ioutil.WriteString("Out.txt", stuff, 0744) | |
// fmt.Println("Done.") | |
} | |
func listFiles(s set, recurse bool, c chan set) { | |
defer wg.Done() | |
files, _ := ioutil.ReadDir(s.path) | |
for _, f := range files { | |
newPath := s.path + "/" + f.Name() | |
if f.IsDir() { | |
wg.Add(1) | |
if recurse { | |
go listFiles(set{s.path + "/" + f.Name(), true, 0}, recurse, c) | |
} | |
// fmt.Print(len(dir), " | ") | |
// fmt.Println("Sending DIR!") | |
c <- set{("D | " + strconv.FormatInt(f.Size(), 10) + " | " + newPath), true, 0} | |
} else { | |
// fmt.Print((len(dir) + len(f.Name())), " | ") | |
c <- set{("F | " + strconv.FormatInt(f.Size(), 10) + " | " + newPath), false, f.Size()} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment