cyclescaler/filerename/main.go

55 lines
932 B
Go
Raw Normal View History

2020-04-04 00:58:46 +00:00
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
)
func main() {
files, err := ioutil.ReadDir("./")
if err != nil {
log.Fatal(err)
}
for _, f := range files {
currentName := f.Name()
if strings.HasSuffix(currentName, ".png") {
if strings.Contains(currentName, "_") {
tmpSplit := strings.Split(currentName, "_")
newName := fmt.Sprintf("combined/%s", tmpSplit[len(tmpSplit)-1])
if _, err := os.Stat(newName); os.IsNotExist(err) {
err := copyFile(currentName, newName)
if err != nil {
log.Printf("failed to rename '%s' to '%s': %v", currentName, newName, err)
}
}
}
}
}
}
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}