Created
January 7, 2018 23:50
-
-
Save slav123/2b3ea9d754df90c953f8d3022da058af to your computer and use it in GitHub Desktop.
update JPG file modification date using EXIF data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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