Implement moving instead of deleting

This commit is contained in:
Jan Bader 2020-11-21 21:43:22 +01:00
parent ebc4a20526
commit 83521eeae6

37
main.go
View File

@ -19,9 +19,42 @@ var fromFile = flag.String("from-file", "", "Load results file from <path>")
var toFile = flag.String("to-file", "", "Save results to <path>") var toFile = flag.String("to-file", "", "Save results to <path>")
var deleteDupesIn = flag.String("delete-dupes-in", "", "Delete duplicates if they are contained in <path>") var deleteDupesIn = flag.String("delete-dupes-in", "", "Delete duplicates if they are contained in <path>")
var promptForDelete = flag.Bool("delete-prompt", false, "Ask which file to keep for each dupe-set") var promptForDelete = flag.Bool("delete-prompt", false, "Ask which file to keep for each dupe-set")
var moveToFolder = flag.String("move-files", "", "Move files to <path> instead of deleting them")
var force = flag.Bool("force", false, "Actually delete files. Without this options, the files to be deleted are only printed") var force = flag.Bool("force", false, "Actually delete files. Without this options, the files to be deleted are only printed")
var verbose = flag.Bool("verbose", false, "Output additional information") var verbose = flag.Bool("verbose", false, "Output additional information")
func Delete(path string) {
if !*force {
return
}
if *moveToFolder == "" {
os.Remove(path)
return
}
MoveButDontOvewrite(path, *moveToFolder)
}
func MoveButDontOvewrite(path string, targetPath string) {
num := 0
filename := filepath.Base(path)
target := filepath.Join(targetPath, filename)
for {
_, err := os.Stat(target)
if os.IsNotExist(err) {
os.Rename(path, target)
return
}
target = filepath.Join(targetPath, filename+"."+strconv.Itoa(num))
num++
}
}
func main() { func main() {
flag.Parse() flag.Parse()
@ -65,7 +98,7 @@ func main() {
if strings.HasPrefix(filepath.Clean(file), deleteIn) { if strings.HasPrefix(filepath.Clean(file), deleteIn) {
fmt.Println("Would delete ", file) fmt.Println("Would delete ", file)
if *force { if *force {
os.Remove(file) Delete(file)
} }
} }
} }
@ -84,7 +117,7 @@ func main() {
for i, file := range duplicateFiles { for i, file := range duplicateFiles {
fmt.Println(i+1, file) fmt.Println(i+1, file)
if *force { if *force {
os.Remove(file) Delete(file)
} }
} }