Compare commits
2 Commits
5bb26c9e15
...
722ba3e1af
Author | SHA1 | Date | |
---|---|---|---|
722ba3e1af | |||
1d2a7aac17 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
main.exe
|
96
main.go
96
main.go
@@ -12,71 +12,32 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetFlags(log.Lmicroseconds)
|
||||
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
||||
}
|
||||
|
||||
func main() {
|
||||
recreate := flag.Bool("f", false, "First rename all files to <name>.bak and then rename them to iter (to prevent conflicts of already existing files)")
|
||||
flag.Parse()
|
||||
|
||||
originalExtensions := make(map[string]string)
|
||||
var mapMutex sync.RWMutex
|
||||
var wg sync.WaitGroup
|
||||
var renamedFiles sync.Map
|
||||
extensionRegex := regexp.MustCompile(`\.[a-zA-Z0-9]+$`)
|
||||
|
||||
if *recreate {
|
||||
log.Printf("Recreating all files first")
|
||||
for _, file := range flag.Args() {
|
||||
wg.Add(1)
|
||||
go func(file string) {
|
||||
defer wg.Done()
|
||||
_, err := os.Stat(file)
|
||||
if os.IsNotExist(err) {
|
||||
log.Printf("File %s does not exist", file)
|
||||
return
|
||||
}
|
||||
|
||||
res := extensionRegex.FindAllString(file, -1)
|
||||
if len(res) == 0 {
|
||||
log.Printf("No extension found for file %s", file)
|
||||
return
|
||||
}
|
||||
dir := path.Dir(file)
|
||||
extension := res[0]
|
||||
bckpFile := fmt.Sprintf("%s/%s.bak", dir, file)
|
||||
|
||||
_, err = os.Stat(bckpFile)
|
||||
if !os.IsNotExist(err) {
|
||||
log.Printf("File %s already exists", bckpFile)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("%s -> %s", file, bckpFile)
|
||||
err = os.Rename(file, bckpFile)
|
||||
if err != nil {
|
||||
log.Printf("Error renaming file %s to %s: %s", file, bckpFile, err)
|
||||
}
|
||||
|
||||
mapMutex.Lock()
|
||||
originalExtensions[bckpFile] = extension
|
||||
mapMutex.Unlock()
|
||||
}(file)
|
||||
}
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
wg = sync.WaitGroup{}
|
||||
for _, file := range flag.Args() {
|
||||
wg.Add(1)
|
||||
go func(file string) {
|
||||
defer wg.Done()
|
||||
if *recreate {
|
||||
file = file + ".bak"
|
||||
file = path.Clean(file)
|
||||
log.Printf("Checking file %s", file)
|
||||
|
||||
if !DoesFileExist(file) {
|
||||
log.Printf("File %s does not exist", file)
|
||||
return
|
||||
}
|
||||
|
||||
info, err := os.Stat(file)
|
||||
if os.IsNotExist(err) {
|
||||
log.Printf("File %s does not exist", file)
|
||||
if info == nil {
|
||||
log.Printf("Error checking file %s: %s", file, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -89,35 +50,35 @@ func main() {
|
||||
modtimeString := modtime.Local().Format("2006-01-02T15-04-05")
|
||||
newFile := modtimeString
|
||||
|
||||
bakless := strings.TrimRight(file, ".bak")
|
||||
res := extensionRegex.FindAllString(bakless, -1)
|
||||
res := extensionRegex.FindAllString(file, -1)
|
||||
if len(res) == 0 {
|
||||
log.Printf("No extension found for file %s", file)
|
||||
return
|
||||
}
|
||||
extension := res[0]
|
||||
|
||||
mapMutex.RLock()
|
||||
prevExtension, ok := originalExtensions[file]
|
||||
mapMutex.RUnlock()
|
||||
if ok {
|
||||
extension = prevExtension
|
||||
}
|
||||
|
||||
dir := path.Dir(file)
|
||||
newFile = fmt.Sprintf("%s/%s%s", dir, newFile, extension)
|
||||
newFile = path.Clean(newFile)
|
||||
newFile = strings.TrimRight(newFile, ".bak")
|
||||
log.Printf("New file name for %s: %s", file, newFile)
|
||||
|
||||
_, err = os.Stat(newFile)
|
||||
if os.IsExist(err) {
|
||||
if DoesFileExist(newFile) {
|
||||
log.Printf("File %s already exists", newFile)
|
||||
return
|
||||
} else if !os.IsNotExist(err) {
|
||||
log.Printf("An unexpected error occurred: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
_, ok := renamedFiles.Load(newFile)
|
||||
if ok {
|
||||
log.Printf("File %s already renamed", newFile)
|
||||
return
|
||||
}
|
||||
|
||||
if file == newFile {
|
||||
log.Printf("File %s already has the correct name", file)
|
||||
return
|
||||
}
|
||||
renamedFiles.Store(newFile, struct{}{})
|
||||
log.Printf("%s -> %s", file, newFile)
|
||||
err = os.Rename(file, newFile)
|
||||
if err != nil {
|
||||
@@ -128,3 +89,12 @@ func main() {
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func DoesFileExist(input string) bool {
|
||||
file, err := os.Open(input)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
file.Close()
|
||||
return true
|
||||
}
|
||||
|
Reference in New Issue
Block a user