mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
gfile.ScanDir支持pattern多个文件模式匹配,使用','符号分隔多个匹配模式
This commit is contained in:
@ -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
|
||||
|
||||
@ -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))
|
||||
}
|
||||
Reference in New Issue
Block a user