72 lines
2 KiB
Go
72 lines
2 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
"velvettear/dedupe/files"
|
|
"velvettear/dedupe/log"
|
|
"velvettear/dedupe/prompts"
|
|
"velvettear/dedupe/settings"
|
|
)
|
|
|
|
func main() {
|
|
timestamp := time.Now()
|
|
settings.Initialize()
|
|
log.Info("starting dedupe...")
|
|
files.Scan()
|
|
if len(files.Duplicates) == 0 {
|
|
log.Info("no duplicate files have been found - exiting...")
|
|
os.Exit(0)
|
|
}
|
|
prompts.ListDuplicates()
|
|
var proceed bool
|
|
var deleteDuplicates bool
|
|
if len(settings.MoveDirectory) == 0 && !settings.Delete {
|
|
exit(timestamp, 0)
|
|
}
|
|
if len(settings.MoveDirectory) > 0 && settings.Delete {
|
|
proceed, deleteDuplicates = prompts.DeleteOrMove()
|
|
} else {
|
|
proceed = prompts.HandleDuplicates(settings.Delete)
|
|
}
|
|
if !proceed {
|
|
exit(timestamp, 0)
|
|
}
|
|
counter := 0
|
|
subtimestamp := time.Now()
|
|
if deleteDuplicates {
|
|
log.Info("deleting duplicate files in '" + settings.ComparisonDirectory + "'...")
|
|
for _, file := range files.Duplicates {
|
|
subsubtimestamp := time.Now()
|
|
error := os.Remove(file)
|
|
if error != nil {
|
|
log.Error("encountered an error deleting the file '"+file+"'", error.Error())
|
|
continue
|
|
}
|
|
counter++
|
|
log.DebugTimed("deleted the file '"+file+"'", subsubtimestamp.UnixMilli())
|
|
}
|
|
log.InfoTimed("deleted "+strconv.Itoa(counter)+" duplicate files to '"+settings.MoveDirectory+"'", subtimestamp.UnixMilli())
|
|
} else {
|
|
log.Info("moving duplicate files to '" + settings.MoveDirectory + "'...")
|
|
for _, file := range files.Duplicates {
|
|
if files.MoveFile(file) {
|
|
counter++
|
|
}
|
|
}
|
|
log.InfoTimed("moved "+strconv.Itoa(counter)+" duplicate files to '"+settings.MoveDirectory+"'", subtimestamp.UnixMilli())
|
|
}
|
|
if prompts.DeleteEmptyDirectories(settings.SourceDirectory) {
|
|
files.RemoveEmptyDirectories(settings.SourceDirectory)
|
|
}
|
|
if prompts.DeleteEmptyDirectories(settings.ComparisonDirectory) {
|
|
files.RemoveEmptyDirectories(settings.ComparisonDirectory)
|
|
}
|
|
exit(timestamp, 0)
|
|
}
|
|
|
|
func exit(timestamp time.Time, code int) {
|
|
log.InfoTimed("dedupe finished - exiting...", timestamp.UnixMilli())
|
|
os.Exit(code)
|
|
}
|