Advertisement
Guest User

Fixed

a guest
Jan 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.00 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/hex"
  5.     "fmt"
  6.     "io/ioutil"
  7.     "log"
  8.     "os"
  9.     "strconv"
  10.     "strings"
  11. )
  12.  
  13. func main() {
  14.     files, err := ioutil.ReadDir(".")
  15.     if err != nil {
  16.         log.Fatal(err)
  17.     }
  18.  
  19.     // loop through the files
  20.     for _, f := range files {
  21.         hexNum := f.Name()
  22.         index := strings.LastIndex(hexNum, "0x")
  23.         if index == -1 {
  24.             continue
  25.         }
  26.         hexNum = hexNum[index+2 : index+4]
  27.  
  28.         // Now we prefix the original name with the actual number.
  29.         numBytes, err := hex.DecodeString(hexNum)
  30.         if err != nil {
  31.             fmt.Printf("Failed to contert %s to base 10: %v\n", hexNum, err)
  32.             continue
  33.         }
  34.  
  35.         num := bytesInt(numBytes)
  36.         newName := strconv.FormatUint(num, 10) + " | " + f.Name()
  37.         // now we finally rename it
  38.  
  39.         err = os.Rename(f.Name(), newName)
  40.         if err != nil {
  41.             fmt.Printf("%s to %s: %v\n", f.Name(), newName, err)
  42.         }
  43.     }
  44. }
  45.  
  46. // return an int that is the total of bytes
  47. func bytesInt(bytes []byte) (ret uint64) {
  48.     for _, b := range bytes {
  49.         ret += uint64(b)
  50.     }
  51.  
  52.     return ret
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement