package dirlist
import (
"encoding/json"
"io/fs"
"os"
"sort"
"time"
)
type FileInfo struct {
Name string // base name of the file
Size int64 // length in bytes for regular files; system-dependent for others
IsDir bool // abbreviation for Mode().IsDir()
Mode fs.FileMode // file mode bits
Mtime time.Time
}
type DirList struct {
Path string
List []*FileInfo
}
func New() *DirList {
return &DirList{
List: make([]*FileInfo, 0),
}
}
func (d *DirList) Read(path string) []*FileInfo {
d.Path = path
dlist, err := os.ReadDir(path)
if err != nil {
return nil
}
files := make([]*FileInfo, 0)
for _, entry := range dlist {
info, err := entry.Info()
if err == nil {
f := new(FileInfo)
f.Name = info.Name()
f.Mtime = info.ModTime()
f.Size = info.Size()
f.Mode = info.Mode()
f.IsDir = info.IsDir()
files = append(files, f)
}
}
d.List = files
return files
}
func (d *DirList) SortByName() []*FileInfo {
files := d.List
sort.Slice(files, func(i, j int) bool {
return files[i].Name > files[j].Name
})
return files
}
func (d *DirList) SortBySize() []*FileInfo {
files := d.List
sort.Slice(files, func(i, j int) bool {
return files[i].Size > files[j].Size
})
return files
}
func (d *DirList) SortByTime() []*FileInfo {
files := d.List
sort.Slice(files, func(i, j int) bool {
return files[i].Mtime.Before(files[j].Mtime)
})
return files
}
func (d *DirList) SortByType() []*FileInfo {
files := d.List
sort.Slice(files, func(i, j int) bool {
return files[i].IsDir
})
return files
}
func (d *DirList) Json() string {
j, err := json.MarshalIndent(d.SortByType(), "", " ")
if err != nil {
return ""
}
return string(j)
}
-
Notifications
You must be signed in to change notification settings - Fork 0
zschong/dirlist
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
获取文件夹内容列表,按名字、时间、大小、类型排序
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published