Skip to content

Instantly share code, notes, and snippets.

@slav123
Created January 7, 2018 23:50
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 slav123/2b3ea9d754df90c953f8d3022da058af to your computer and use it in GitHub Desktop.
Save slav123/2b3ea9d754df90c953f8d3022da058af to your computer and use it in GitHub Desktop.
update JPG file modification date using EXIF data
package main
import (
"fmt"
"github.com/rwcarlsen/goexif/exif"
"io/ioutil"
"os"
"path"
"path/filepath"
)
func main() {
//argsWithProg := os.Args
argsWithoutProg := os.Args[1:]
if len(argsWithoutProg) < 0 {
fmt.Println("Give me a directory")
os.Exit(0)
} else {
dirname := os.Args[1]
files, err := ioutil.ReadDir(dirname)
if err != nil {
panic(err)
}
for _, file := range files {
fpath := path.Join(dirname, file.Name())
if filepath.Ext(fpath) == ".JPEG" || filepath.Ext(fpath) == ".JPG" {
println(fpath)
f, err := os.Open(fpath)
if err != nil {
panic(err)
}
defer f.Close()
x, err := exif.Decode(f)
if err != nil {
fmt.Println("Failed to decode ", fpath)
panic(err)
}
tm, _ := x.DateTime()
fmt.Println(tm.Date())
err = os.Chtimes(fpath, tm, tm)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Changed the file time information")
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment