mirror of
https://github.com/JaCoB1123/dupe-finder.git
synced 2025-05-18 14:11:55 +02:00
Move funcs to other file
This commit is contained in:
parent
f16a143125
commit
8a9bcbf62e
39
file.go
Normal file
39
file.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func remove(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++
|
||||||
|
}
|
||||||
|
}
|
36
main.go
36
main.go
@ -23,38 +23,6 @@ var moveToFolder = flag.String("move-files", "", "Move files to <path> instead o
|
|||||||
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()
|
||||||
|
|
||||||
@ -98,7 +66,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 {
|
||||||
delete(file)
|
remove(file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -138,7 +106,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if *force {
|
if *force {
|
||||||
delete(file)
|
remove(file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user