Skip to content

Instantly share code, notes, and snippets.

@lpar
Last active March 3, 2018 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lpar/b7297324b225807d4e34ed9e12393ccb to your computer and use it in GitHub Desktop.
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
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