commit 58bba8958861eb72ae9583676a4d176246b050b7 Author: Felipe Martin Date: Mon Aug 5 19:01:53 2019 +0200 0.1.0 diff --git a/README.md b/README.md new file mode 100644 index 0000000..219a092 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +RNMR +==== + +Easily rename files based on its creation date. + +``` +git clone https://github.com/fmartingr/rnmr.git +go build +./rnmr -h +``` diff --git a/main.go b/main.go new file mode 100644 index 0000000..31e81be --- /dev/null +++ b/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "flag" + "fmt" + "os" + "path/filepath" + "strings" +) + +const defaultAllowedExtensions = "jpg,jpeg,png,gif,avi,mp4,mov" + +var extensions = flag.String("extensions", defaultAllowedExtensions, "Comma separated extensions to allow") + +func stringInSlice(a string, list []string) bool { + for _, b := range list { + if b == a { + return true + } + } + return false +} + +func isValidFile(extension string) bool { + allowedExtensions := strings.Split(*extensions, ",") + return stringInSlice(extension[1:], allowedExtensions) +} + +func readDir(path string, f os.FileInfo, err error) error { + extension := filepath.Ext(f.Name()) + if !f.IsDir() && extension != "" && isValidFile(extension) { + filenameDate := f.ModTime().Format("2006-01-02_15-04-05") + newPath := filepath.Dir(path) + "/" + filenameDate + extension + if path != newPath { + fmt.Printf("%s => %s\n", path, newPath) + err := os.Rename(path, newPath) + if err != nil { + panic(err) + } + } + } + return nil +} + +func main() { + flag.Parse() + + path, err := filepath.Abs(filepath.Dir(os.Args[0])) + if err != nil { + panic(err) + } + + fmt.Printf("Walking by %s...\n", path) + + err2 := filepath.Walk(path, readDir) + if err2 != nil { + panic(err2) + } +}