batch_op_file/main.go

68 lines
1.3 KiB
Go
Raw Normal View History

2024-12-19 19:58:06 +08:00
package main
import (
"fmt"
"os"
"path/filepath"
2025-01-01 18:02:27 +08:00
"strings"
2024-12-19 19:58:06 +08:00
)
func main() {
2025-01-01 18:02:27 +08:00
var paths []string
rootDir, _ := os.Getwd()
paths, _ = GetAllFile(rootDir, paths, []string{".git", ".idea", ".vscode", ".gitignore", ".gitea", ".github", ".golangci.yml", "*.pyc"})
2024-12-19 19:58:06 +08:00
2025-01-01 18:02:27 +08:00
for _, fp := range paths {
fmt.Println(fp)
2024-12-19 19:58:06 +08:00
}
}
2025-01-01 18:02:27 +08:00
func GetAllFile(pathname string, s []string, filter []string) ([]string, error) {
rd, err := os.ReadDir(pathname)
if err != nil {
fmt.Println("read dir fail:", err)
return s, err
}
for _, fi := range rd {
// 检查文件名是否匹配任何一个过滤模式
matched := false
for _, item := range filter {
exists, err := filepath.Match(item, fi.Name())
if err != nil {
continue
}
if exists {
matched = true
break
}
}
2024-12-19 19:58:06 +08:00
2025-01-01 18:02:27 +08:00
if matched {
continue
2024-12-19 19:58:06 +08:00
}
2025-01-01 18:02:27 +08:00
if fi.IsDir() {
fullDir := pathname + "/" + fi.Name()
s, err = GetAllFile(fullDir, s, filter)
if err != nil {
fmt.Println("read dir fail:", err)
return s, err
2024-12-19 19:58:06 +08:00
}
2025-01-01 18:02:27 +08:00
} else {
fullName := pathname + "/" + fi.Name()
s = append(s, fullName)
2024-12-19 19:58:06 +08:00
}
}
2025-01-01 18:02:27 +08:00
return s, nil
}
2024-12-19 19:58:06 +08:00
2025-01-01 18:02:27 +08:00
func In(target string, array []string) bool {
target = strings.Trim(target, "")
for _, v := range array {
if strings.Trim(v, "") == target {
return true
}
2024-12-19 19:58:06 +08:00
}
2025-01-01 18:02:27 +08:00
return false
2024-12-19 19:58:06 +08:00
}