Skip to content

Instantly share code, notes, and snippets.

@crossworth
Created March 30, 2020 23:30
Show Gist options
  • Save crossworth/06ce70daa0f9b8eac352f4097dfeefdd to your computer and use it in GitHub Desktop.
Save crossworth/06ce70daa0f9b8eac352f4097dfeefdd to your computer and use it in GitHub Desktop.
sort file by mod time
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