-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleaner.go
166 lines (155 loc) · 4.12 KB
/
cleaner.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"COMMON/db"
//"github.com/djherbis/times"
"flag"
"log"
"os"
"path/filepath"
"strconv"
"time"
)
var dbFile = "right.db"
func main() {
//rightPath := "./public" // the one we KEEP
//leftPath := "./left" // the one we can REMOVE
// === PARAMETERS
usage := `USAGE=
cleaner -left=[path to folder where duplicates will be REMOVED !] -right=[path to master folder (NOTHING gets removed in that one...)]
`
paramsLeftPtr := flag.String("left", "", "folder to CLEAN (ie: duplicate files here will be removed!")
paramsRightPtr := flag.String("right", "", "folder to ANALYSE (ie: that's your master folder, nothing will be removed from here...")
flag.Parse()
tail := flag.Args()
if *paramsLeftPtr == "" {
log.Println("Parameters incomplete (missing -left=...).")
log.Println(usage)
return
}
if *paramsRightPtr == "" {
log.Println("Parameters incomplete (missing -right=...).")
log.Println(usage)
return
}
if len(tail) != 0 {
log.Println("Wrong Parameters (too many).")
log.Println(usage)
return
}
leftPath := *paramsLeftPtr
rightPath := *paramsRightPtr
// === GO !
err := os.Remove(dbFile)
if err != nil {
log.Fatal(err)
return
}
var createStmts []string
// TABLE: master
createStmts = append(createStmts, `
CREATE TABLE IF NOT EXISTS right (
Id INTEGER PRIMARY KEY,
Name TEXT NOT NULL,
Size INT,
ModifDate TEXT,
Path TEXT
)
;
`)
if err := db.InitDB(dbFile, createStmts); err != nil {
log.Println("ERROR: ", err.Error())
return
}
// ANALYSE right (generate DB)
target(rightPath)
log.Println("============================================")
log.Println("============================================")
log.Println("============================================")
// REMOVE from left
source(leftPath)
}
func target(searchDir string) {
cnt := 0
log.Println("will check folder (right): ", searchDir)
//fileList := []string{}
err := filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
log.Println("***")
/*
log.Println("R-Path: ", path)
log.Println("R-IsDir: ", f.IsDir())
log.Println("R-Name: ", f.Name())
log.Println("R-Size: ", f.Size())
log.Println("R-ModTime: ", f.ModTime())
*/
if !f.IsDir() {
add := `INSERT INTO right (Name, Size, ModifDate, Path)
VALUES('` + f.Name() + `', '` + strconv.FormatInt(f.Size(), 10) + `', '` + f.ModTime().Format(time.RFC3339) + `', '` + path + `')
`
db.Write(add)
log.Println("=> Written! ", add)
cnt++
}
/*
t, err := times.Stat(path)
if err != nil {
log.Fatal(err.Error())
}
log.Println("Birth time: ", t.BirthTime())
log.Println("Modification Time: ", t.ModTime())
log.Println("Change Time: ", t.ChangeTime())
log.Println("Access Time: ", t.AccessTime())
*/
return nil
})
if err != nil {
log.Fatal(err)
}
log.Println("+++ +++ +++")
log.Println("SUMMARY RIGHT: # of FILES= ", cnt)
log.Println("+++ +++ +++")
return
}
func source(searchDir string) {
cnt := 0
log.Println("will analyse folder (left): ", searchDir)
//fileList := []string{}
err := filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
log.Println("***")
log.Println("L-Path: ", path)
log.Println("L-IsDir: ", f.IsDir())
log.Println("L-Name: ", f.Name())
log.Println("L-Size: ", f.Size())
log.Println("L-ModTime: ", f.ModTime())
if !f.IsDir() {
sel := `SELECT Name, Size, ModifDate FROM right
WHERE
Name = '` + f.Name() + `'
AND Size = '` + strconv.FormatInt(f.Size(), 10) + `'
AND ModifDate = '` + f.ModTime().Format(time.RFC3339) + `'
`
res, err := db.Select(sel)
if err != nil {
log.Fatal(err)
return err
}
// Print results (not mandatory here, as we only check if exists YES or NO)
cntRows := len(res)
log.Println("LEN= ", cntRows)
if cntRows != 0 {
log.Println("===> REMOVE file: ", path)
if err := os.Remove(path); err != nil {
log.Fatal("ERROR trying to remove: ", path, err)
}
cnt++
}
}
return nil
})
if err != nil {
log.Fatal(err)
}
log.Println("+++ +++ +++")
log.Println("SUMMARY LEFT: # of files REMOVED= ", cnt)
log.Println("+++ +++ +++")
return
}