Skip to content

Instantly share code, notes, and snippets.

@vsivsi
Created June 20, 2023 19:49
Show Gist options
  • Save vsivsi/0c1d0c5d92db294a072440e24532c821 to your computer and use it in GitHub Desktop.
Save vsivsi/0c1d0c5d92db294a072440e24532c821 to your computer and use it in GitHub Desktop.
Rename files in CWD to remove characters forbidden by OneDrive
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