Golang给目录按时间排序.正则、时间条件搜索文件
发布时间:2023-03-12 编辑:jiaochengji.com
教程集为您提供Golang给目录按时间排序.正则、时间条件搜索文件等资源,欢迎您收藏本站,我们将为您提供最新的Golang给目录按时间排序.正则、时间条件搜索文件资源
package filepath
import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
//path表示搜索的路径,FullDir表示是不是递归查询,MatchDir表示是否匹配目录.
type FindFiles struct {
Path string `json:path`
FullDir bool `json:fulldir`
MatchDir bool `json:matchdir`
}
//date小于等于0的时候表示查找最近这段时间的文件
func (self FindFiles) DateFindFile(date int64) ([]string, error) {
date = date * 24 * 60 * 60
var less bool
switch {
case date <= 0:
date = time.Now().Unix() date
less = true
case date > 0:
date = time.Now().Unix() - date
less = false
}
return datewalk(date, less, self.FullDir, self.MatchDir, self.Path)
}
func (self FindFiles) RegFindFile(reg string) ([]string, error) {
if strings.Index(reg, "*") == 0 {
reg = "." reg
} else {
reg = "^" reg
}
reg = "$"
Reg, err := regexp.Compile(reg)
if err != nil {
return []string{}, nil
}
if self.FullDir {
return namewalk(Reg, self.MatchDir, self.Path)
}
var list []string
infos, err := readDir(self.Path)
if err != nil {
return list, nil
}
path := filepath.ToSlash(self.Path)
if !strings.HasSuffix(path, "/") {
path = "/"
}
for _, v := range infos {
if Reg.MatchString(v.Name()) {
if v.IsDir() && !self.MatchDir {
continue
}
list = append(list, path v.Name())
}
}
return list, nil
}
func (self FindFiles) DateAndRegexp(date int64, reg string) ([]string, error) {
var l []string
list, err := self.RegFindFile(reg)
if err != nil {
return l, err
}
date = date * 24 * 60 * 60
var less bool = false
if date <= 0 {
date = time.Now().Unix() date
less = true
} else {
date = time.Now().Unix() - date
}
for _, v := range list {
info, err := os.Stat(v)
if err != nil {
continue
}
if less {
if date > info.ModTime().Unix() {
continue
}
} else {
if date < info.ModTime().Unix() {
continue
}
}
l = append(l, v)
}
return l, nil
}
func datewalk(date int64, less bool, fulldir, matchdir bool, path string) ([]string, error) {
var list []string
if !strings.HasSuffix(path, "/") {
path = "/"
}
if !fulldir {
infos, err := readDir(path)
if err != nil {
return list, err
}
for _, info := range infos {
file, ok := dResolve(date, less, matchdir, path, info)
if ok {
file = path file
list = append(list, file)
}
}
return list, nil
}
return list, filepath.Walk(path, func(root string, info os.FileInfo, err error) error {
if err != nil {
return err
}
_, ok := dResolve(date, less, matchdir, root, info)
if ok {
root = filepath.ToSlash(root)
list = append(list, root)
}
return nil
})
}
func dResolve(date int64, less, matchdir bool, root string, info os.FileInfo) (string, bool) {
if less {
if date > info.ModTime().Unix() {
return "", false
}
} else {
if date < info.ModTime().Unix() {
return "", false
}
}
root = filepath.ToSlash(root)
if info.IsDir() && !matchdir {
return "", false
}
return info.Name(), true
}
func namewalk(reg *regexp.Regexp, matchdir bool, path string) ([]string, error) {
var list []string
return list, filepath.Walk(path, func(root string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !reg.MatchString(info.Name()) {
return nil
}
root = filepath.ToSlash(root)
if info.IsDir() && !matchdir {
return nil
}
list = append(list, root)
return nil
})
}
func readDir(path string) ([]os.FileInfo, error) {
info, err := os.Stat(path)
if err != nil {
return nil, err
}
if info.IsDir() {
return ioutil.ReadDir(path)
}
return []os.FileInfo{info}, nil
}
/*
type fileInfo []os.FileInfo
func (self fileInfo) Less(i, j int) bool {
return self[i].ModTime().Unix() > self[j].ModTime().Unix()
}
func (self fileInfo) Len() int {
return len(self)
}
func (self fileInfo) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
func ReadDir(path string) ([]os.FileInfo, error) {
list, err := readDir(path)
if err != nil {
return nil, err
}
sort.Sort(fileInfo(list))
return list, err
}
*/
到此这篇关于“Golang给目录按时间排序.正则、时间条件搜索文件”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!
您可能感兴趣的文章:
dedecms中tag列表按最新发布时间排序的解决方法
如何提高网站的搜索排名
第三代google排名搜索引擎技术与P2P
网站排名基本原则
Google简介
PHP遍历目录下的TXT文件且按时间排序
高性能mysql学习笔记总结
认识搜索引擎优化
“HillTop”论 - 探索Google排名新算法(一)
用Golang写一个搜索引擎(0x0B)--- 第一部分结束
[关闭]