-
Notifications
You must be signed in to change notification settings - Fork 0
/
moveFiles.go
60 lines (48 loc) · 1.32 KB
/
moveFiles.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
package filefriend
import (
"io/ioutil"
"os"
)
// MoveFiles moves all the files in given slice to destination.
// Uses the passed in destination parameter to select folder to move to.
// Cleanup will delete all trailing folders that are empty after move.
//
// Returns the updated slice containing, all the files or potensial error that occurred.
func MoveFiles(files []*File, dest string, cleanup bool) error {
// create folder if not exist
dest = SanitizePath(dest)
err := CreateFolder(dest)
if err != nil {
return err
}
for _, file := range files {
// get new and old paths
newPath := dest + file.name + file.extension
oldPath := file.path + "\\" + file.name + file.extension
// move path (from -> to)
moved := Move(oldPath, newPath)
// handle potensial error occurring during move
if moved != nil {
return moved
}
// if 'clenup' flag is set to true
// check if old folder is empty
// if its empty, remove it
dirFiles, err := ioutil.ReadDir(file.folder)
if err != nil {
return err
}
// delete folder if empty after move
if len(dirFiles) == 0 {
os.Remove(file.folder)
}
// if no errors, get new updated file info
updatedFileInfo, err := GetFileInfo(newPath)
if err != nil {
return err
}
// set updated file info at old file
*file = *updatedFileInfo
}
return nil
}