Skip to content

Instantly share code, notes, and snippets.

@eko
Created July 30, 2016 09:45
Show Gist options
  • Save eko/6b0caaefeaf82f2aa202804743040292 to your computer and use it in GitHub Desktop.
Save eko/6b0caaefeaf82f2aa202804743040292 to your computer and use it in GitHub Desktop.
Photo renamer in Golang based on EXIF format
package main
import (
"flag"
"fmt"
"github.com/rwcarlsen/goexif/exif"
"os"
"path"
"path/filepath"
"strings"
"time"
)
var counter int = 0
func visit(pathname string, f os.FileInfo, err error) error {
openedFile, _ := os.Open(pathname)
exifData, err := exif.Decode(openedFile)
if err != nil {
fmt.Printf("x Unable to load EXIF data for file: %s", pathname)
return nil
}
exifDatetime, _ := exifData.DateTime()
newFilename := fmt.Sprintf("%s%s", exifDatetime.Format("20060102030405"), path.Ext(pathname))
newPathname := strings.ToLower(strings.Replace(pathname, f.Name(), newFilename, 1))
os.Rename(pathname, newPathname)
fmt.Printf("✓ Renamed to: %s\n", newPathname)
counter += 1
return nil
}
func main() {
flag.Parse()
directory := flag.Arg(0)
startTime := time.Now()
filepath.Walk(directory, visit)
endTime := time.Now()
fmt.Printf("\n\nʕ◔ϖ◔ʔ I successfully renamed %d photos in %s", counter, endTime.Sub(startTime))
}
@eko
Copy link
Author

eko commented Apr 4, 2020

Great, you're welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment