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 Jul 30, 2016

This script renames your photos in the following format (based on EXIF datetime): YYYYMMDDHHMMSS.jpg.

Simply run the following command:

$ go run renamer.go /Users/vincent/photos/

✓ Renamed to: /users/vincent/photos/20150726114143.jpg
✓ Renamed to: /users/vincent/photos/20150802062648.jpg
x Unable to load EXIF data for file: /Users/vincent/photos/IMG_2260.MOV
...

ʕ◔ϖ◔ʔ I successfully renamed 300 photos in 191.559797ms

@yorch
Copy link

yorch commented Sep 17, 2018

Thanks for the script, very useful!

I updated it to support images with the exact same datestamp (like when continuously shooting):
https://gist.github.com/yorch/bba4249e692bf329a9cc705ec824705a

@yorch
Copy link

yorch commented Apr 4, 2020

Created a repo for this script:
https://github.com/yorch/photo-renamer

@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