Skip to content

Instantly share code, notes, and snippets.

@MrOplus
Created September 5, 2020 12:22
Show Gist options
  • Save MrOplus/ebd70da73de72da407efd99ff7ee7412 to your computer and use it in GitHub Desktop.
Save MrOplus/ebd70da73de72da407efd99ff7ee7412 to your computer and use it in GitHub Desktop.
Regex Folder Rename
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
func main() {
root := "Y:\\Serials\\Serials Complete\\Sherlock"
seriesName := "Sherlock"
var files []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
files = append(files, path)
return nil
})
if err != nil {
panic(err)
}
r := regexp.MustCompile(`[Ss](?P<Season>\d{2}).*[Ee](?P<Episode>\d{2})`)
//names := r.SubexpNames()
for _, file := range files {
res := r.FindAllStringSubmatch(file,-1)
if len(res) > 0 {
s := res[0][1]
e := res[0][2]
newFileName := fmt.Sprintf("%s Seasson %s Episode %s.%s",seriesName,s,e,file[strings.LastIndex(file,".") +1:])
_ = os.Rename(file, root+"\\"+newFileName)
fmt.Println(newFileName)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment