Last active
March 3, 2018 15:49
-
-
Save lpar/b7297324b225807d4e34ed9e12393ccb to your computer and use it in GitHub Desktop.
Rename radio shows downloaded from BBC iPlayer with get_iplayer to have human-friendly filenames
This file contains 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 | |
// This is a quick Go program to rename radio shows downloaded from BBC iPlayer | |
// by removing the program IDs and _original from the end of the filename | |
// and converting underscores to spaces. | |
// Copyright status: WTFPL http://www.wtfpl.net | |
import ( | |
"fmt" | |
"os" | |
"path/filepath" | |
"regexp" | |
"strings" | |
) | |
var crudRemove = regexp.MustCompile(`_[a-z0-9]+_(original|default)`) | |
func renamer(fspc string, info os.FileInfo, err error) error { | |
if err != nil { | |
fmt.Printf("%s: %s", fspc, err) | |
return err | |
} | |
if crudRemove.FindString(fspc) != "" { | |
x := crudRemove.ReplaceAllString(fspc, "") | |
newfspc := strings.Replace(x, "_", " ", -1) | |
fmt.Printf("%s → %s\n", fspc, newfspc) | |
err := os.Rename(fspc, newfspc) | |
return err | |
} | |
return nil | |
} | |
func main() { | |
for _, fspc := range os.Args[1:] { | |
err := filepath.Walk(fspc, renamer) | |
if err != nil { | |
fmt.Printf("%s: %s", fspc, err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment