GOLANG – 查找在日期范围内创建的文件

huangapple go评论115阅读模式
英文:

GOLANG - Find files created between a date range

问题

我想知道如何在指定文件夹中按日期范围查找文件。例如:我想查找文件夹X中在2013年8月1日至2013年8月31日期间创建的所有文件。

我尝试了以下代码:

  1. dir := "path/to/dir"
  2. t, err := time.Parse("2006-01-02T15:04:05-07:00", "2018-04-07T05:48:03+08:00")
  3. if err != nil {
  4. panic(err)
  5. }
  6. paths, infos, err := FindFilesAfter(dir, t)
  7. if err != nil {
  8. panic(err)
  9. }
  10. for i, _ := range paths {
  11. checkFile(paths[i], infos[i])
  12. }
  13. func FindFilesAfter(dir string, t time.Time) (paths []string, infos []os.FileInfo, err error) {
  14. err = filepath.Walk(dir, func(p string, i os.FileInfo, e error) error {
  15. if e != nil {
  16. return e
  17. }
  18. if !i.IsDir() && i.ModTime().After(t) {
  19. paths = append(paths, p)
  20. infos = append(infos, i)
  21. }
  22. return nil
  23. })
  24. return
  25. }

请注意,这是一段示例代码,你需要将"path/to/dir"替换为实际的文件夹路径,并根据需要修改日期范围。这段代码将遍历指定文件夹及其子文件夹,找到在指定日期之后创建的文件,并将它们的路径和信息存储在pathsinfos中。你可以根据自己的需求进一步处理这些文件。

英文:

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:

  1. dir := "path/to/dir"
  2. t, err := time.Parse("2006-01-02T15:04:05-07:00", "2018-04-07T05:48:03+08:00")
  3. if err != nil {
  4. panic(err)
  5. }
  6. paths, infos, err := FindFilesAfter(dir, t)
  7. if err != nil {
  8. panic(err)
  9. }
  10. for i, _ := range paths {
  11. checkFile(paths[i], infos[i])
  12. }
  13. func FindFilesAfter(dir string, t time.Time) (paths []string, infos []os.FileInfo, err error) {
  14. err = filepath.Walk(dir, func(p string, i os.FileInfo, e error) error {
  15. if e != nil {
  16. return e
  17. }
  18. if !i.IsDir() && i.ModTime().After(t) {
  19. paths = append(paths, p)
  20. infos = append(infos, i)
  21. }
  22. return nil
  23. })
  24. return
  25. }

答案1

得分: 0

希望以下回答是你所寻找的。

  • 如果你的问题更多关于时间范围,你可以使用time包中的BeforeAfter函数。
  • 如果你的问题更多关于查找创建时间而不是修改时间,你可以考虑使用syscall包来查找atime、mtime和ctime,它们的含义如下:

atime(访问时间)是文件的访问时间

mtime(修改时间)是文件的修改时间

ctime(更改时间)是inode或文件的更改时间

  1. package main
  2. import (
  3. "io/fs"
  4. "log"
  5. "os"
  6. "syscall"
  7. "time"
  8. )
  9. func main() {
  10. // 准备数据
  11. start, _ := time.Parse(time.RFC3339, "2022-11-26T07:04:05Z")
  12. end, _ := time.Parse(time.RFC3339, "2022-11-26T08:10:00Z")
  13. var dir = "your path"
  14. files := FindFilesByDateRange(dir, start, end)
  15. // 打印结果
  16. log.Printf("文件范围:%s-%s\n", start.Format(time.RFC3339), end.Format(time.RFC3339))
  17. for _, f := range files {
  18. log.Println(f.Name())
  19. }
  20. }
  21. func FindFilesByDateRange(dir string, start, end time.Time) []fs.FileInfo {
  22. fileSystem := os.DirFS(dir)
  23. var files []fs.FileInfo
  24. if err := fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
  25. if err != nil {
  26. log.Fatal(err)
  27. }
  28. fileInfo, err := d.Info()
  29. if err != nil {
  30. return err
  31. }
  32. stat := fileInfo.Sys().(*syscall.Stat_t)
  33. cDate := time.Unix(stat.Ctimespec.Sec, stat.Ctimespec.Nsec).UTC()
  34. if !d.IsDir() && (cDate.After(start) && cDate.Before(end)) {
  35. files = append(files, fileInfo)
  36. }
  37. return nil
  38. }); err != nil {
  39. return nil
  40. }
  41. return files
  42. }
英文:

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 and After from time 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

  1. package main
  2. import (
  3. "io/fs"
  4. "log"
  5. "os"
  6. "syscall"
  7. "time"
  8. )
  9. func main() {
  10. // prepare data
  11. start, _ := time.Parse(time.RFC3339, "2022-11-26T07:04:05Z")
  12. end, _ := time.Parse(time.RFC3339, "2022-11-26T08:10:00Z")
  13. var dir = "your path"
  14. files := FindFilesByDateRange(dir, start, end)
  15. // print result
  16. log.Printf("file range: %s-%s\n", start.Format(time.RFC3339), end.Format(time.RFC3339))
  17. for _, f := range files {
  18. log.Println(f.Name())
  19. }
  20. }
  21. func FindFilesByDateRange(dir string, start, end time.Time) []fs.FileInfo {
  22. fileSystem := os.DirFS(dir)
  23. var files []fs.FileInfo
  24. if err := fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
  25. if err != nil {
  26. log.Fatal(err)
  27. }
  28. fileInfo, err := d.Info()
  29. if err != nil {
  30. return err
  31. }
  32. stat := fileInfo.Sys().(*syscall.Stat_t)
  33. cDate := time.Unix(stat.Ctimespec.Sec, stat.Ctimespec.Nsec).UTC()
  34. if !d.IsDir() && (cDate.After(start) && cDate.Before(end)) {
  35. files = append(files, fileInfo)
  36. }
  37. return nil
  38. }); err != nil {
  39. return nil
  40. }
  41. return files
  42. }

huangapple
  • 本文由 发表于 2022年11月25日 21:28:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/74573561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定