From 777be19931f9d15e8bf663112551f82657d6954b Mon Sep 17 00:00:00 2001 From: NiseVoid Date: Sat, 17 Sep 2016 19:32:27 +0200 Subject: [PATCH] Initial commit --- main.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..b69a0ca --- /dev/null +++ b/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "fmt" + "hash/crc32" + "io" + "os" + "path/filepath" + "regexp" + "strings" +) + +func main() { + regex, _ := regexp.Compile(` \[[A-Z0-9]{8}\]$`) + files := os.Args[1:] + + for _, name := range files { + file, err := os.Open(name) + if err != nil { + fmt.Printf(`Couldn't open file "%s", exiting ...`, name) + os.Exit(1) + } + + ext := filepath.Ext(name) + basename := name[0 : len(name)-len(ext)] + if regex.MatchString(basename) { + basename = basename[0 : len(basename)-11] + } + + hash := crc32.NewIEEE() + + io.Copy(hash, file) + + err = file.Close() + if err != nil { + fmt.Printf(`Couldn't close file "%s", exiting ...`, name) + os.Exit(1) + } + + sum := fmt.Sprintf(`%X`, hash.Sum32()) + sum = strings.Repeat(`0`, 8-len(sum)) + sum + + err = os.Rename(name, fmt.Sprintf("%s [%s]%s", basename, sum, ext)) + if err != nil { + fmt.Printf(`Couldn't rename file "%s", exiting ...`, name) + os.Exit(1) + } + } +}