batch_op_file/util/main.go

58 lines
1.0 KiB
Go
Raw Normal View History

2025-01-01 18:09:00 +08:00
package util
2024-12-19 19:58:06 +08:00
import (
"fmt"
"os"
"path/filepath"
2025-01-01 18:02:27 +08:00
"strings"
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
}