英文:
GOLANG - Find files created between a date range
问题
我想知道如何在指定文件夹中按日期范围查找文件。例如:我想查找文件夹X中在2013年8月1日至2013年8月31日期间创建的所有文件。
我尝试了以下代码:
dir := "path/to/dir"
t, err := time.Parse("2006-01-02T15:04:05-07:00", "2018-04-07T05:48:03+08:00")
if err != nil {
panic(err)
}
paths, infos, err := FindFilesAfter(dir, t)
if err != nil {
panic(err)
}
for i, _ := range paths {
checkFile(paths[i], infos[i])
}
func FindFilesAfter(dir string, t time.Time) (paths []string, infos []os.FileInfo, err error) {
err = filepath.Walk(dir, func(p string, i os.FileInfo, e error) error {
if e != nil {
return e
}
if !i.IsDir() && i.ModTime().After(t) {
paths = append(paths, p)
infos = append(infos, i)
}
return nil
})
return
}
请注意,这是一段示例代码,你需要将"path/to/dir"
替换为实际的文件夹路径,并根据需要修改日期范围。这段代码将遍历指定文件夹及其子文件夹,找到在指定日期之后创建的文件,并将它们的路径和信息存储在paths
和infos
中。你可以根据自己的需求进一步处理这些文件。
英文:
I'd like to know how to find files in a specific folder between a date range. For example: I want to find all files in folder X that were created between 01-Aug-13 and 31-Aug-13.
I tried this:
dir := "path/to/dir"
t, err := time.Parse("2006-01-02T15:04:05-07:00", "2018-04-07T05:48:03+08:00")
if err != nil {
panic(err)
}
paths, infos, err := FindFilesAfter(dir, t)
if err != nil {
panic(err)
}
for i, _ := range paths {
checkFile(paths[i], infos[i])
}
func FindFilesAfter(dir string, t time.Time) (paths []string, infos []os.FileInfo, err error) {
err = filepath.Walk(dir, func(p string, i os.FileInfo, e error) error {
if e != nil {
return e
}
if !i.IsDir() && i.ModTime().After(t) {
paths = append(paths, p)
infos = append(infos, i)
}
return nil
})
return
}
答案1
得分: 0
希望以下回答是你所寻找的。
- 如果你的问题更多关于时间范围,你可以使用
time
包中的Before
和After
函数。 - 如果你的问题更多关于查找创建时间而不是修改时间,你可以考虑使用
syscall
包来查找atime、mtime和ctime,它们的含义如下:
atime(访问时间)是文件的访问时间
mtime(修改时间)是文件的修改时间
ctime(更改时间)是inode或文件的更改时间
package main
import (
"io/fs"
"log"
"os"
"syscall"
"time"
)
func main() {
// 准备数据
start, _ := time.Parse(time.RFC3339, "2022-11-26T07:04:05Z")
end, _ := time.Parse(time.RFC3339, "2022-11-26T08:10:00Z")
var dir = "your path"
files := FindFilesByDateRange(dir, start, end)
// 打印结果
log.Printf("文件范围:%s-%s\n", start.Format(time.RFC3339), end.Format(time.RFC3339))
for _, f := range files {
log.Println(f.Name())
}
}
func FindFilesByDateRange(dir string, start, end time.Time) []fs.FileInfo {
fileSystem := os.DirFS(dir)
var files []fs.FileInfo
if err := fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
log.Fatal(err)
}
fileInfo, err := d.Info()
if err != nil {
return err
}
stat := fileInfo.Sys().(*syscall.Stat_t)
cDate := time.Unix(stat.Ctimespec.Sec, stat.Ctimespec.Nsec).UTC()
if !d.IsDir() && (cDate.After(start) && cDate.Before(end)) {
files = append(files, fileInfo)
}
return nil
}); err != nil {
return nil
}
return files
}
英文:
Hope the following answer is what you are looking for.
- If your point of question is more about time range you can using function
Before
andAfter
fromtime
package - If your point of question is more about finding create time instead of modifying time. you can consider package syscall to find atime, mtime, and ctime - in essence those are:
atime (access time) is the file access time
mtime (modify time) is the file modify time
ctime (change time) is the inode or file change time
package main
import (
"io/fs"
"log"
"os"
"syscall"
"time"
)
func main() {
// prepare data
start, _ := time.Parse(time.RFC3339, "2022-11-26T07:04:05Z")
end, _ := time.Parse(time.RFC3339, "2022-11-26T08:10:00Z")
var dir = "your path"
files := FindFilesByDateRange(dir, start, end)
// print result
log.Printf("file range: %s-%s\n", start.Format(time.RFC3339), end.Format(time.RFC3339))
for _, f := range files {
log.Println(f.Name())
}
}
func FindFilesByDateRange(dir string, start, end time.Time) []fs.FileInfo {
fileSystem := os.DirFS(dir)
var files []fs.FileInfo
if err := fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
log.Fatal(err)
}
fileInfo, err := d.Info()
if err != nil {
return err
}
stat := fileInfo.Sys().(*syscall.Stat_t)
cDate := time.Unix(stat.Ctimespec.Sec, stat.Ctimespec.Nsec).UTC()
if !d.IsDir() && (cDate.After(start) && cDate.Before(end)) {
files = append(files, fileInfo)
}
return nil
}); err != nil {
return nil
}
return files
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论