神不在的星期二
func ControlDeepWalk(basepath string, count int, hard, debug bool) error { var ( stock = make(map[string][]string) countBase = 0 correctdirs []string ) base := filepath.Base(basepath) dirbase := filepath.Dir(basepath) countBase = len(strings.Split(filepath.Dir(basepath), "/")) if debug { log.Printf("countbase: %v %v\n", strings.Split(filepath.Dir(basepath), "/"), countBase) log.Printf("base :%v : %v : %v\n", base, dirbase, strings.Split(basepath, "/")) } err := filepath.WalkDir(basepath, func(path string, d fs.DirEntry, err error) error { if err != nil { if debug { log.Printf("--error inf walkdir function, exit") } return err } if d.IsDir() { if filepath.Dir(path) == filepath.Dir(basepath) { if debug { log.Printf("found root directory, skipping %v : %v\n", filepath.Dir(path), filepath.Dir(basepath)) } } else { compare := false if hard { compare = len(strings.Split(filepath.Dir(path), "/")) == countBase+count } else { compare = len(strings.Split(filepath.Dir(path), "/")) <= countBase+count } if compare { if debug { log.Printf("-found dir: [%v] %v\n", path, d.Name()) } stock[filepath.Dir(filepath.Join(path, d.Name()))] = []string{} correctdirs = append(correctdirs, filepath.Dir(filepath.Join(path, d.Name()))) } } } else { fdir, ffile := filepath.Split(path) for _, x := range correctdirs { if x == filepath.Dir(fdir) { if debug { log.Printf("-found file:%v : %v %v %v \n", d.Name(), path, fdir, ffile) } stock[x] = append(stock[x], d.Name()) } } } return nil }) if debug { for k, v := range stock { log.Printf("%v : %v \n", k, v) } log.Printf("%v\n", stock) } return err}func main() { p := "/backup/backuper/test" count := 2 _ = ControlDeepWalk(p, count, false, true)}