Home  >  Article  >  Backend Development  >  GOLANG - Find files created within a certain date range

GOLANG - Find files created within a certain date range

WBOY
WBOYforward
2024-02-09 22:30:08998browse

GOLANG - 查找在某个日期范围内创建的文件

In our daily work, we often need to find files created within a certain date range. In GOLANG, we can use some built-in functions and libraries to achieve this function. In this article, PHP editor Xiaoxin will introduce how to use the GOLANG programming language to implement the function of finding files within a specified date range. Whether it is for daily work or personal projects, this function is very practical, let us find out together!

Question content

I would like to know how to find files in a specific folder within a certain date range. For example: I want to find all files in folder x that were created between August 1, 2013 and August 31, 2013.

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
}

Solution

Hope the following answer is what you are looking for. ​​p>

  • If your question is more about time range, you can use the functions time from package before and after
  • If your question is more about finding creation time rather than modification time. You might consider using package syscall to find atime, mtime and ctime - essentially they are:

atime (access time) is the file access time

mtime (modification time) is the file modification 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
}

The above is the detailed content of GOLANG - Find files created within a certain date range. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete