-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (167 loc) · 4.54 KB
/
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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
_ "errors"
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
)
type Item struct {
Name, Path, Type string
}
type Items struct {
Data []Item
}
var allcount int
var limit int
/******/
func (rows *Items) AddItem(row Item) []Item {
rows.Data = append(rows.Data, row)
return rows.Data
} // end func
/******/
func main() {
var mode string
var inputDir string
var outputDir string
// define option flag and parse
flag.StringVar(&mode, "mode", "copy", "Mode : copy / move")
flag.StringVar(&inputDir, "in", "FILES", "Directory to Scan")
flag.StringVar(&outputDir, "out", "OUTPUT", "Directory to store result")
flag.IntVar(&limit, "limit", 100, "Limit of max files to execute")
flag.Parse()
/****/
dirname := "./" + inputDir
newDir := outputDir
// before begin operation, make sure output folder was created
MakeOutputDir(outputDir)
// main operation
ProsesDir(dirname, newDir, mode)
} // end func
func ProsesDir(dirname, newDir, mode string) {
if allcount > limit {
log.Println("Limit Reached - only can process max", limit, "files, to bypass limit please add flag -limit NUMBER")
return
}
result, count, err := scanDir(dirname)
if err != nil {
log.Println("ERROR detected -", err)
}
allcount = allcount + count
/** for now can't use this, maybe later **/
// check if any file to proccess
// if allcount > 1 {
// log.Println("-- Files Detected :", allcount, "file(s) --")
// } else {
// log.Println("-- Empty Folder! Can't Find Files to Process Or File Already Moved --")
// return
// }
// looping each file and folder from scanned result
i := 1
for _, source := range result {
// check limit for first directory
if allcount == 0 {
if i >= limit {
log.Println("Limit Reached - only can process max", limit, "files, to bypass limit please add flag -limit NUMBER")
i++
return
}
}
// execute
if source.Type == "file" {
// if scanned result is file, decide next step based on opt flag to copy or move files
if mode == "move" {
err := MoveToDir(source.Name, source.Path, newDir)
if err != nil {
log.Println("ERROR on", source.Name, "-", err)
}
} else if mode == "copy" {
err := CopyToDir(source.Name, source.Path, newDir)
if err != nil {
log.Println("ERROR on", source.Name, "-", err)
}
} else {
log.Println("-- ERROR Invalid Mode! please use -mode flag with copy or paste", mode, "--")
return
}
} else {
// if scanned result was a directory, recursive scan that directory
ProsesDir(dirname+"/"+source.Name, newDir, mode)
}
}
} // end func
/** we scan whats inside directory **/
func scanDir(dirname string) ([]Item, int, error) {
items := []Item{}
all := Items{items}
path, _ := os.Getwd()
count := 0
// scanning directoruy here
files, err := ioutil.ReadDir(dirname)
if err != nil {
return items, count, err
}
for _, file := range files {
tipe := "folder"
// if detected as file
if file.IsDir() == false {
tipe = "file"
count++
}
// store to array struct
temp := Item{
Name: file.Name(),
Path: filepath.Join(path, dirname+"/"),
Type: tipe,
}
all.AddItem(temp)
}
return all.Data, count, nil
} // end func
/** if output folder not found, create it **/
func MakeOutputDir(path string) {
dirpath, _ := os.Getwd()
fpath := filepath.Join(dirpath, "./"+path)
if _, err := os.Stat(fpath); os.IsNotExist(err) {
// create new directory if not exist
os.Mkdir(fpath, os.ModePerm)
log.Println("-- Creating", path, "directory --")
}
} // end func
/** move file inside folder to output folder **/
func MoveToDir(filename, dir, newDir string) error {
path, _ := os.Getwd()
// define old location and new location to moving file
oldLocation := filepath.Join(dir, filename)
newLocation := filepath.Join(path, newDir+"/"+filename)
// execute
err := os.Rename(oldLocation, newLocation)
if err != nil {
// log.Println(err)
return err
}
log.Println(filename, "Moved!")
return nil
} // end func
/** copy file inside folder to output folder **/
func CopyToDir(filename, dir, newDir string) error {
path, _ := os.Getwd()
// define file fullpath and target to copying file
sourceFile := filepath.Join(dir, filename)
destinationFile := filepath.Join(path, newDir+"/"+filename)
// read content of source file
input, err := ioutil.ReadFile(sourceFile)
if err != nil {
// log.Println(err)
return err
}
// write content to target destination file
err = ioutil.WriteFile(destinationFile, input, 0644)
if err != nil {
// log.Println(err)
return err
}
log.Println(filename, "Copied!")
return nil
} // end func