I am using the Filepath.Walk in Go to try to get all the folders under C: recursively. However it just returns me sub-folders starting with $Recycle.Bin. What am I doing wrong here?
package main
import (
"fmt"
"path/filepath"
"os"
)
func main() {
array := Subfolders("C:")
for _,value := range array {
fmt.Println(value)
}
}
func Subfolders(path string) (paths []string) {
filepath.Walk(path, func(newPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
paths = append(paths, newPath)
}
return nil
})
return paths
}
Output:
C:
C:\$Recycle.Bin
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$R0L9M20
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$R22ZOD9
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$R3LS9P4
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$R4T2IGU
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$R8TZIET
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$R9QQZB9
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$RA71HY3
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$RBOC0V4.com
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$RBOC0V4.com\go-fsnotify
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$REJFS3Z
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$RF9A1Y6
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$ROMESWQ
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$RP4CYID
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$RQHMPV5
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$RV0K99H
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$RX54T04
I have many other folders under C: which donot get picked up by the filepath.walk. I wanted to understand what was the reason behind it.
EDIT
Thanks to the answers I was able to resolve the issue as below:-
func Subfolders(path string) (paths []string) {
filepath.Walk(path, func(newPath string, info os.FileInfo, err error) error {
if err != nil {
log.Println(err)
return filepath.SkipDir
}
if info.IsDir() {
paths = append(paths, newPath)
}
return nil
})