-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (46 loc) · 1002 Bytes
/
main.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
package main
import (
"os"
"path/filepath"
"sync"
"github.com/longbridgeapp/opencc"
)
func convertPath(converter *opencc.OpenCC, p string, wg *sync.WaitGroup) {
wg.Add(1)
defer wg.Done()
items, err := os.ReadDir(p)
if (err != nil) {
panic(err)
}
for _, item := range items {
wg.Add(1)
go func(item os.DirEntry) {
defer wg.Done()
newName, _ := converter.Convert(item.Name())
oldPath := filepath.Join(p, item.Name())
newPath := filepath.Join(p, newName)
err := os.Rename(oldPath, newPath)
if (err != nil) {
println("Fail to rename file: " + item.Name())
} else {
println(newPath)
}
if (item.IsDir()) {
go convertPath(converter, newPath, wg)
}
}(item)
}
}
func main() {
path := os.Args[1]
if path == "" {
panic("Path is required")
}
converter, err := opencc.New("s2t")
if err != nil {
panic(err)
}
wg := &sync.WaitGroup{}
convertPath(converter, path, wg)
wg.Wait()
}