Make cleaner work with all date videos
This commit is contained in:
@@ -6,51 +6,62 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
logger "git.site.quack-lab.dev/dave/cylogger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
logger.InitFlag()
|
||||
if flag.NArg() == 0 {
|
||||
fmt.Println("Usage: cleaner <files>")
|
||||
os.Exit(1)
|
||||
}
|
||||
// regex to match " - 2025-07-08 01h31m45s - "
|
||||
re := regexp.MustCompile(` - (\d{4}-\d{2}-\d{2} \d{2}h\d{2}m\d{2}s) - `)
|
||||
// regex to match "2025-07-08"
|
||||
re := regexp.MustCompile(`(\d{4}-\d{2}-\d{2})`)
|
||||
|
||||
for _, file := range flag.Args() {
|
||||
filelog := logger.Default.WithPrefix(file)
|
||||
filelog.Info("Processing file")
|
||||
|
||||
info, err := os.Stat(file)
|
||||
if err != nil {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
filelog.Error("ERROR: %v\n", err)
|
||||
continue
|
||||
}
|
||||
if info.IsDir() {
|
||||
fmt.Printf("SKIP (directory): %s\n", file)
|
||||
filelog.Info("SKIP (directory): %s\n", file)
|
||||
continue
|
||||
}
|
||||
|
||||
name := filepath.Base(file)
|
||||
match := re.FindStringSubmatch(name)
|
||||
filelog.Debug("Match: %v", match)
|
||||
if match == nil {
|
||||
fmt.Printf("SKIP (no date pattern): %s\n", name)
|
||||
filelog.Info("SKIP (no date pattern): %s\n", name)
|
||||
continue
|
||||
}
|
||||
|
||||
newName := match[1] + filepath.Ext(name)
|
||||
filelog.Debug("New name: %s", newName)
|
||||
if name == newName {
|
||||
fmt.Printf("SKIP (already named): %s\n", name)
|
||||
filelog.Info("SKIP (already named): %s\n", name)
|
||||
continue
|
||||
}
|
||||
|
||||
filelog.Debug("Checking if target exists: %s", newName)
|
||||
if _, err := os.Stat(newName); err == nil {
|
||||
fmt.Printf("SKIP (target exists): %s -> %s\n", name, newName)
|
||||
filelog.Info("SKIP (target exists): %s -> %s\n", name, newName)
|
||||
continue
|
||||
}
|
||||
|
||||
filelog.Info("Renaming to: %s", newName)
|
||||
err = os.Rename(name, newName)
|
||||
if err != nil {
|
||||
fmt.Printf("ERROR renaming %s: %v\n", name, err)
|
||||
filelog.Error("ERROR renaming %s: %v\n", name, err)
|
||||
} else {
|
||||
fmt.Printf("RENAMED: %s -> %s\n", name, newName)
|
||||
filelog.Info("RENAMED: %s -> %s\n", name, newName)
|
||||
}
|
||||
filelog.Info("All done")
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user