Initial commit
This commit is contained in:
commit
777be19931
49
main.go
Normal file
49
main.go
Normal 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…
Reference in New Issue
Block a user