Created
June 20, 2023 19:49
-
-
Save vsivsi/0c1d0c5d92db294a072440e24532c821 to your computer and use it in GitHub Desktop.
Rename files in CWD to remove characters forbidden by OneDrive
This file contains 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
func main() { | |
dir := "./" | |
bad := "\"*:<>?/\\|" | |
files, err := os.ReadDir(dir) | |
if err != nil { | |
panic(err) | |
} | |
mapfunc := func (c rune) rune { | |
if strings.ContainsRune(bad, c) { | |
return '_' | |
} | |
return c | |
} | |
c := 1 | |
for _, file := range files { | |
if strings.ContainsAny(file.Name(), "\"*:<>?/\\|") { | |
fmt.Println(">", c, file.Name()) | |
newfn := strings.Map(mapfunc, file.Name()) | |
fmt.Println("<", c, newfn) | |
if err = os.Rename(dir+file.Name(), dir+newfn); err != nil { | |
panic(err) | |
} | |
c++ | |
} | |
} | |
fmt.Println(c-1, "Bad filenames") | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment