Created
October 28, 2016 15:48
-
-
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.
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" | |
"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