Created
March 30, 2020 23:30
-
-
Save crossworth/06ce70daa0f9b8eac352f4097dfeefdd to your computer and use it in GitHub Desktop.
sort file by mod time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"path/filepath" | |
"time" | |
"sort" | |
"gopkg.in/djherbis/times.v1" | |
"os" | |
) | |
type fileInfo struct { | |
name string | |
modtime time.Time | |
} | |
func main() { | |
files := []fileInfo{} | |
matches, _ := filepath.Glob("./*.mp4") | |
for _, match := range matches { | |
file, err := times.Stat(match) | |
if err != nil { | |
fmt.Println(err) | |
continue; | |
} | |
f := fileInfo{ | |
name: match, | |
modtime: file.BirthTime(), | |
} | |
files = append(files, f) | |
} | |
sort.Slice(files, func(i, j int) bool { | |
return files[i].modtime.Unix() < files[j].modtime.Unix() | |
}) | |
for i, el := range files { | |
name := fmt.Sprintf("%d - %v", i, el.name) | |
os.Rename(el.name, name) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment