57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
)
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
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) - `)
|
|
|
|
for _, file := range flag.Args() {
|
|
info, err := os.Stat(file)
|
|
if err != nil {
|
|
fmt.Printf("ERROR: %v\n", err)
|
|
continue
|
|
}
|
|
if info.IsDir() {
|
|
fmt.Printf("SKIP (directory): %s\n", file)
|
|
continue
|
|
}
|
|
name := filepath.Base(file)
|
|
match := re.FindStringSubmatch(name)
|
|
if match == nil {
|
|
fmt.Printf("SKIP (no date pattern): %s\n", name)
|
|
continue
|
|
}
|
|
|
|
newName := match[1] + filepath.Ext(name)
|
|
if name == newName {
|
|
fmt.Printf("SKIP (already named): %s\n", name)
|
|
continue
|
|
}
|
|
|
|
if _, err := os.Stat(newName); err == nil {
|
|
fmt.Printf("SKIP (target exists): %s -> %s\n", name, newName)
|
|
continue
|
|
}
|
|
|
|
err = os.Rename(name, newName)
|
|
if err != nil {
|
|
fmt.Printf("ERROR renaming %s: %v\n", name, err)
|
|
} else {
|
|
fmt.Printf("RENAMED: %s -> %s\n", name, newName)
|
|
}
|
|
continue
|
|
}
|
|
}
|