-
Notifications
You must be signed in to change notification settings - Fork 0
/
shake_files2.go
88 lines (77 loc) · 1.65 KB
/
shake_files2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"os"
"log"
"math/rand"
"time"
"flag"
"regexp"
"path"
"strconv"
)
func main() {
var dirname string
var rename bool
var rollback bool
var renamedir bool
flag.StringVar(&dirname,"p", ".", "path to directory with files")
flag.BoolVar(&rename,"w", false, "Rename files (Default: Just print result without rename files)")
flag.BoolVar(&rollback,"r", false, "Delete prefix in filenames if it exist")
flag.BoolVar(&renamedir,"d", false, "Rename dirs too")
flag.Parse()
fdirname, err := os.Open(dirname)
if err != nil {
log.Fatal(err)
}
fnames, err := fdirname.Readdirnames(-1)
if err != nil {
log.Fatal(err)
}
//Not shaked
// for n,fname := range fnames {
// fmt.Println(n,fname)
// }
// fmt.Println("Shaked:")
//shake
var tmp string
rsour := rand.New(rand.NewSource(time.Now().UnixNano()))
for n,fname := range fnames {
tmp = fname
r := rsour.Intn(len(fnames)-1)
fnames[n] = fnames[r]
fnames[r] = tmp
}
//show shaked
// for n,fname := range fnames {
// fmt.Println(n,fname)
// }
var number int
for _,fname := range fnames {
if ! renamedir {
fi, err := os.Stat(path.Join(dirname,fname))
if err != nil {
log.Fatal(err)
}
if fi.Mode().IsDir() {
continue
}
}
prf := "$2"
if ! rollback {
prf = strconv.Itoa(number)
prf += "RND-$2"
}
re := regexp.MustCompile(`^(\d+RND-)?(.+)$`)
new_fname := re.ReplaceAllString(fname, prf)
if rename {
err = os.Rename(path.Join(dirname,fname),path.Join(dirname,new_fname))
if err != nil {
log.Fatal(err)
}
} else {
fmt.Printf("%-50s ---> %s\n",path.Join(dirname,fname),path.Join(dirname,new_fname))
}
number++
}
}