Skip to content

Instantly share code, notes, and snippets.

@wuman
Created September 1, 2016 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wuman/2ae2d188a200148b7e022c2df5e963e6 to your computer and use it in GitHub Desktop.
Save wuman/2ae2d188a200148b7e022c2df5e963e6 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
)
func main() {
files, err := ioutil.ReadDir(".")
if err != nil {
log.Fatalf("Could not read current directory: %v", err)
}
validName := regexp.MustCompile(`^((19|20)\d\d)-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])_([01]\d|2[0-3])-([0-5]\d)-([0-5]\d).(jpg|jpeg)`)
for _, f := range files {
if !validName.MatchString(f.Name()) {
continue
}
parts := validName.FindStringSubmatch(f.Name())
y, month, d, h, minute, s, ext := parts[1], parts[3], parts[4], parts[5], parts[6], parts[7], parts[8]
name := fmt.Sprintf("%s-%s-%s %s.%s.%s.%s", y, month, d, h, minute, s, ext)
err := os.Rename(f.Name(), name)
fmt.Printf("%s => %s %v\n", f.Name(), name, err == nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment