Created
September 5, 2020 12:22
-
-
Save MrOplus/ebd70da73de72da407efd99ff7ee7412 to your computer and use it in GitHub Desktop.
Regex Folder Rename
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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