Compare commits
3 Commits
d4be1566e2
...
bebff64bda
| Author | SHA1 | Date | |
|---|---|---|---|
|
bebff64bda
|
|||
|
0b006b426d
|
|||
|
a545e8318b
|
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.idea/
|
||||
out/
|
||||
!out/.gitkeep
|
||||
7
Makefile
Normal file
7
Makefile
Normal file
@@ -0,0 +1,7 @@
|
||||
#!make
|
||||
|
||||
build-linux:
|
||||
GOOS=linux go build -o out/ff_renamer main.go
|
||||
|
||||
build-windows:
|
||||
GOOS=windows go build -o out/ff_renamer.exe main.go
|
||||
106
main.go
Normal file
106
main.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Pair struct {
|
||||
originFull string
|
||||
newName string
|
||||
newFull string
|
||||
skip bool
|
||||
}
|
||||
|
||||
var (
|
||||
exts = []string{
|
||||
".jpg", ".jpeg", ".jfif",
|
||||
".png", ".gif", ".webp",
|
||||
".mp4", ".MP4", ".webm",
|
||||
}
|
||||
|
||||
renameSlice []Pair
|
||||
|
||||
pattern, _ = regexp.Compile("^\\d+(-\\d)?$") // 123456789, 123456789-1
|
||||
)
|
||||
|
||||
func generateName(baseName string) string {
|
||||
counter := 1
|
||||
name := baseName
|
||||
|
||||
loop:
|
||||
for {
|
||||
for _, pair := range renameSlice {
|
||||
if pair.newName == name {
|
||||
name = fmt.Sprintf("%s-%d", baseName, counter)
|
||||
counter++
|
||||
continue loop
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
return name
|
||||
}
|
||||
|
||||
func main() {
|
||||
entries, err := os.ReadDir(".")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
renameSlice = make([]Pair, 0)
|
||||
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
ext := filepath.Ext(entry.Name())
|
||||
extL := strings.ToLower(ext)
|
||||
if !slices.Contains(exts, extL) {
|
||||
continue
|
||||
}
|
||||
|
||||
if pattern.MatchString(strings.TrimSuffix(entry.Name(), ext)) {
|
||||
renameSlice = append(renameSlice, Pair{
|
||||
originFull: entry.Name(),
|
||||
skip: true})
|
||||
continue
|
||||
}
|
||||
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
newName := generateName(strconv.FormatInt(info.ModTime().Unix(), 10))
|
||||
|
||||
renameSlice = append(renameSlice, Pair{
|
||||
originFull: entry.Name(),
|
||||
newName: newName,
|
||||
newFull: newName + extL})
|
||||
}
|
||||
|
||||
counter := 0
|
||||
for i, pair := range renameSlice {
|
||||
if pair.skip || pair.originFull == pair.newFull {
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("%d. %s --> %s\n", i+1, pair.originFull, pair.newFull)
|
||||
err := os.Rename(pair.originFull, pair.newFull)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
counter++
|
||||
}
|
||||
|
||||
fmt.Printf("Total: %d\n", len(renameSlice))
|
||||
fmt.Printf("Renamed: %d\n", counter)
|
||||
}
|
||||
35
script.sh
35
script.sh
@@ -1,35 +0,0 @@
|
||||
#!bash
|
||||
|
||||
PERL_SCRIPT=$(cat <<'EOF'
|
||||
@files = split(/:/, $ARGV[0]);
|
||||
@new_files = ();
|
||||
foreach $file (@files) {
|
||||
if ($file =~ m#^\./.+\.(jpe?g|png|gif)$#) {
|
||||
($mtime) = (stat($file))[9];
|
||||
$ext = $1;
|
||||
$new_file = "./$mtime.$ext";
|
||||
if ($new_file eq $file) {
|
||||
next;
|
||||
}
|
||||
while (grep $_ eq $new_file, @new_files) {
|
||||
if (! defined $suffix) {
|
||||
$suffix = "-1";
|
||||
} else {
|
||||
$suffix = $suffix - 1;
|
||||
}
|
||||
$new_file = "./$mtime$suffix.$ext";
|
||||
}
|
||||
push(@new_files, $new_file);
|
||||
$file =~ s#([ \(\)])#\\$1#g;
|
||||
print "mv -n \"$file\" $new_file\n";
|
||||
undef $suffix;
|
||||
}
|
||||
}
|
||||
$count = @new_files;
|
||||
print "echo \"Renamed: $count\"\n";
|
||||
EOF
|
||||
)
|
||||
|
||||
renamer() {
|
||||
find . -maxdepth 1 -type f -print | tr '\n' ':' | xargs -I{} -- perl -e "$PERL_SCRIPT" '{}' | xargs -n1 -I{} -- bash -c '{}'
|
||||
}
|
||||
Reference in New Issue
Block a user