Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/hex"
- "fmt"
- "io/ioutil"
- "log"
- "os"
- "strconv"
- "strings"
- )
- func main() {
- files, err := ioutil.ReadDir(".")
- if err != nil {
- log.Fatal(err)
- }
- // loop through the files
- for _, f := range files {
- hexNum := f.Name()
- index := strings.LastIndex(hexNum, "0x")
- if index == -1 {
- continue
- }
- hexNum = hexNum[index+2 : index+4]
- // Now we prefix the original name with the actual number.
- numBytes, err := hex.DecodeString(hexNum)
- if err != nil {
- fmt.Printf("Failed to contert %s to base 10: %v\n", hexNum, err)
- continue
- }
- num := bytesInt(numBytes)
- newName := strconv.FormatUint(num, 10) + " | " + f.Name()
- // now we finally rename it
- err = os.Rename(f.Name(), newName)
- if err != nil {
- fmt.Printf("%s to %s: %v\n", f.Name(), newName, err)
- }
- }
- }
- // return an int that is the total of bytes
- func bytesInt(bytes []byte) (ret uint64) {
- for _, b := range bytes {
- ret += uint64(b)
- }
- return ret
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement