Last active
March 28, 2018 12:20
-
-
Save micheltlutz/8398f801abf3ac61ff002dd5be7caeb4 to your computer and use it in GitHub Desktop.
Renomear arquivos removendo acento e espaço - GO
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" | |
| "unicode" | |
| "golang.org/x/text/transform" | |
| "golang.org/x/text/unicode/norm" | |
| ) | |
| func main() { | |
| dir := "<colocar seu diretorio>" | |
| files, err := ioutil.ReadDir(dir) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| for _, f := range files { | |
| rename(dir+f.Name(), dir+normalize(f.Name())) | |
| } | |
| } | |
| func normalize(nomeArquivo string) string { | |
| t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC) | |
| nomeNovo, _, err := transform.String(t, nomeArquivo) | |
| if err != nil { | |
| log.Println(err) | |
| } | |
| fmt.Println(string(nomeNovo)) | |
| return string(nomeNovo) | |
| } | |
| func isMn(r rune) bool { | |
| return unicode.Is(unicode.Mn, r) || unicode.IsSpace(r) | |
| } | |
| func rename(nomeVelho string, nomeNovo string) { | |
| err := os.Rename(nomeVelho, nomeNovo) | |
| if err != nil { | |
| fmt.Println(err) | |
| return | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment