package main
import (
"fmt"
"io/ioutil"
"os"
"path"
)
func findEmptyFolder(dirname string) (emptys []string, err error) {
// Golang学习 - io/ioutil 包
// https://www.cnblogs.com/golove/p/3278444.html
files, err := ioutil.ReadDir(dirname)
if err != nil {
return nil, err
}
// 判断底下是否有文件
if len(files) == 0 {
return []string{dirname}, nil
}
for _, file := range files {
if file.IsDir() {
edirs, err := findEmptyFolder(path.Join(dirname, file.Name()))
if err != nil {
return nil, err
}
if edirs != nil {
emptys = append(emptys, edirs...)
}
}
}
return emptys, nil
}
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage:", os.Args[0], "起始路径")
return
}
emptys, err := findEmptyFolder(os.Args[1])
if err != nil {
fmt.Println(err.Error())
return
}
for _, dir := range emptys {
if err := os.Remove(dir); err != nil {
fmt.Println("错误:", err.Error())
} else {
fmt.Println("删除成功:", dir)
}
}
}