Skip to content

Instantly share code, notes, and snippets.

@darron
Created October 28, 2016 15:48
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 darron/d0476ad6f3f1073e8810dcf2767c8722 to your computer and use it in GitHub Desktop.
Save darron/d0476ad6f3f1073e8810dcf2767c8722 to your computer and use it in GitHub Desktop.
Rename a bunch of files that were downloaded from S3 with really long names.
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)
var (
theDir = "/Users/darron/Desktop/Movies"
)
func main() {
movieDir, err := ioutil.ReadDir(theDir)
if err != nil {
log.Fatal("Problem opening the directory.")
}
fileNames := getFileNames(movieDir)
if fileNames == nil {
log.Fatal("We have no files to deal with.")
}
splitNames(fileNames)
}
func getFileNames(files []os.FileInfo) []string {
var names []string
for _, file := range files {
names = append(names, file.Name())
}
return names
}
func splitNames(files []string) {
for _, v := range files {
pieces := strings.Split(v, "?")
originalPath := fmt.Sprintf("%s/%s", theDir, v)
newPath := fmt.Sprintf("%s/%s", theDir, pieces[0])
// fmt.Printf("Orig: %s\nNew: %s\n\n", originalPath, newPath)
err := os.Rename(originalPath, newPath)
if err != nil {
log.Fatal("Oh boy.")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment