A crappy crc32 tool written in Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
925 B

  1. package main
  2. import (
  3. "fmt"
  4. "hash/crc32"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "regexp"
  9. "strings"
  10. )
  11. func main() {
  12. regex, _ := regexp.Compile(` \[[A-Z0-9]{8}\]$`)
  13. files := os.Args[1:]
  14. for _, name := range files {
  15. file, err := os.Open(name)
  16. if err != nil {
  17. fmt.Printf(`Couldn't open file "%s", exiting ...`, name)
  18. os.Exit(1)
  19. }
  20. ext := filepath.Ext(name)
  21. basename := name[0 : len(name)-len(ext)]
  22. if regex.MatchString(basename) {
  23. basename = basename[0 : len(basename)-11]
  24. }
  25. hash := crc32.NewIEEE()
  26. io.Copy(hash, file)
  27. err = file.Close()
  28. if err != nil {
  29. fmt.Printf(`Couldn't close file "%s", exiting ...`, name)
  30. os.Exit(1)
  31. }
  32. sum := fmt.Sprintf(`%X`, hash.Sum32())
  33. sum = strings.Repeat(`0`, 8-len(sum)) + sum
  34. err = os.Rename(name, fmt.Sprintf("%s [%s]%s", basename, sum, ext))
  35. if err != nil {
  36. fmt.Printf(`Couldn't rename file "%s", exiting ...`, name)
  37. os.Exit(1)
  38. }
  39. }
  40. }