Initial commit

This commit is contained in:
Nise Void 2016-09-17 19:32:27 +02:00
commit 777be19931

49
main.go Normal file
View File

@ -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)
}
}
}