Created
February 16, 2013 16:58
-
-
Save dchest/4967684 to your computer and use it in GitHub Desktop.
Renames URL-encoded filenames like %FU%CK%IN%G0 to normal text.
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 ( | |
| "flag" | |
| "fmt" | |
| "net/url" | |
| "os" | |
| "path/filepath" | |
| ) | |
| func main() { | |
| flag.Parse() | |
| if flag.NArg() != 1 { | |
| fmt.Fprintln(os.Stderr, "usage: fileurldec path/to/directory") | |
| os.Exit(1) | |
| } | |
| rootPath := flag.Arg(0) | |
| paths := make([]string, 0) | |
| err := filepath.Walk(rootPath, func (path string, info os.FileInfo, err error) error { | |
| paths = append(paths, path) | |
| return nil | |
| }) | |
| if err != nil { | |
| fmt.Fprintf(os.Stderr, "%s", err) | |
| os.Exit(1) | |
| } | |
| for _, path := range paths { | |
| fullPath, err := filepath.Abs(path) | |
| if err != nil { | |
| fmt.Fprintf(os.Stderr, "* ERROR: %s", err) | |
| continue | |
| } | |
| name := filepath.Base(fullPath) | |
| decodedName, err := url.QueryUnescape(name) | |
| if err != nil { | |
| fmt.Fprintf(os.Stderr, "* ERROR: %s", err) | |
| continue | |
| } | |
| newPath := filepath.Join(filepath.Dir(fullPath), decodedName) | |
| if newPath == fullPath { | |
| continue | |
| } | |
| // Rename! | |
| if err := os.Rename(fullPath, newPath); err != nil { | |
| fmt.Fprintf(os.Stderr, "%s", err) | |
| continue | |
| } | |
| fmt.Printf("Renamed %s\n", newPath) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment