1

Compare commits

...

3 Commits

Author SHA1 Message Date
bebff64bda feat: переход на Go 2024-05-15 03:12:36 +03:00
0b006b426d commit 2022-02-18 19:15:13 +03:00
a545e8318b commit 2020-07-26 02:40:41 +03:00
5 changed files with 119 additions and 35 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
.idea/
out/
!out/.gitkeep

7
Makefile Normal file
View 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

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module ff_renamer
go 1.21

106
main.go Normal file
View 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)
}

View File

@@ -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 '{}'
}