gfile.ScanDir支持pattern多个文件模式匹配,使用','符号分隔多个匹配模式

This commit is contained in:
john
2018-10-26 18:39:03 +08:00
parent 8315ada9db
commit 810252a00e
2 changed files with 8 additions and 13 deletions

View File

@ -251,7 +251,8 @@ func ScanDir(path string, pattern string, recursive ... bool) ([]string, error)
return list, nil
}
// 内部检索目录方法,支持递归,返回没有排序的文件绝对路径列表结果
// 内部检索目录方法,支持递归,返回没有排序的文件绝对路径列表结果
// pattern参数支持多个文件名称模式匹配使用','符号分隔多个模式。
func doScanDir(path string, pattern string, recursive ... bool) ([]string, error) {
var list []string
// 打开目录
@ -275,8 +276,10 @@ func doScanDir(path string, pattern string, recursive ... bool) ([]string, error
}
}
// 满足pattern才加入结果列表
if match, err := filepath.Match(pattern, name); err == nil && match {
list = append(list, path)
for _, p := range strings.Split(pattern, ",") {
if match, err := filepath.Match(strings.TrimSpace(p), name); err == nil && match {
list = append(list, path)
}
}
}
return list, nil

View File

@ -2,17 +2,9 @@ package main
import (
"gitee.com/johng/gf/g"
"gitee.com/johng/gf/g/net/ghttp"
"gitee.com/johng/gf/g/os/gfile"
)
func main() {
s := g.Server()
s.BindHandler("/{class}-{course}/:name/*act", func(r *ghttp.Request){
r.Response.Writeln(r.Get("class"))
r.Response.Writeln(r.Get("course"))
r.Response.Writeln(r.Get("name"))
r.Response.Writeln(r.Get("act"))
})
s.SetPort(8199)
s.Run()
g.Dump(gfile.ScanDir("/var/log", "*.log, *.gz", true))
}