-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-find-md5-sum-of-file-then-rename-it.go
66 lines (52 loc) · 1.12 KB
/
2-find-md5-sum-of-file-then-rename-it.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"crypto/md5"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"
)
var tempPath = filepath.Join(os.Getenv("TEMP"), "temp-concurrency-pipeline")
func main() {
log.Println("start")
start := time.Now()
proceed()
duration := time.Since(start)
log.Println("done in", duration.Seconds(), "seconds")
}
func proceed() {
counterTotal := 0
counterRenamed := 0
err := filepath.Walk(tempPath, func(path string, info os.FileInfo, err error) error {
// if there is an error, return immediatelly
if err != nil {
return err
}
// if it is a sub directory, return immediatelly
if info.IsDir() {
return nil
}
counterTotal++
// read file
buf, err := ioutil.ReadFile(path)
if err != nil {
return err
}
// sum it
sum := fmt.Sprintf("%x", md5.Sum(buf))
// rename file
destinationPath := filepath.Join(tempPath, fmt.Sprintf("file-%s.txt", sum))
err = os.Rename(path, destinationPath)
if err != nil {
return err
}
counterRenamed++
return nil
})
if err != nil {
log.Println("ERROR:", err.Error())
}
log.Printf("%d/%d files renamed", counterRenamed, counterTotal)
}