50 lines
925 B
Go
50 lines
925 B
Go
|
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)
|
||
|
}
|
||
|
}
|
||
|
}
|