Ai
1 Star 0 Fork 0

water/gobase

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
find_file_service.go 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
leijmdas 提交于 2024-09-13 20:42 +08:00 . add
package fileutils
import (
"github.com/sirupsen/logrus"
"os"
"path/filepath"
"strings"
)
type FindFileService struct {
dir string
excludeSuffix string
includeSuffix string
}
func NewFindFileService() *FindFileService {
return &FindFileService{
dir: FindRootDir(),
excludeSuffix: ".docx,.exe",
}
}
func (self *FindFileService) Dir() string {
return self.dir
}
func (self *FindFileService) SetDir(dir string) {
self.dir = dir
}
func (self *FindFileService) IncludeSuffix() string {
return self.includeSuffix
}
func (self *FindFileService) SetIncludeSuffix(includeSuffix string) {
self.includeSuffix = includeSuffix
}
func (self *FindFileService) ExcludeSuffix() string {
return self.excludeSuffix
}
func (self *FindFileService) SetExcludeSuffix(excludeSuffix string) {
self.excludeSuffix = excludeSuffix
}
func (self *FindFileService) FindFiles() ([]string, error) {
//var dirPath = fileutils.FindRootDirGoMod() + consts.Webcmd
var notIncludes = strings.Split(self.excludeSuffix, ",")
var includes = strings.Split(self.includeSuffix, ",")
var files = make([]string, 0)
// 指定需要遍历的目录
// 使用filepath.Walk遍历目录
err := filepath.Walk(self.dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
logrus.Error(err)
return err
}
// 如果是文件,可以进行额外操作,比如读取文件内容
if !info.IsDir() {
for _, filter := range notIncludes {
if strings.HasSuffix(path, filter) {
return nil
}
}
for _, match := range includes {
if !strings.HasSuffix(path, match) {
return nil
}
}
files = append(files, path)
}
// 返回nil继续遍历
return nil
})
if err != nil {
logrus.Error("Error walking the path:", err)
}
return files, err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/leijmdas/gobase.git
git@gitee.com:leijmdas/gobase.git
leijmdas
gobase
gobase
636798dbe43d

搜索帮助