Browse Source

Initial commit

master
Nise Void 4 years ago
commit
777be19931
1 changed files with 49 additions and 0 deletions
  1. +49
    -0
      main.go

+ 49
- 0
main.go 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)
}
}
}

Loading…
Cancel
Save