Skip to content

Instantly share code, notes, and snippets.

@ryancraigmartin
Created January 26, 2024 16:13
Show Gist options
  • Save ryancraigmartin/1fa54832f62703e25a6359b0b8981d4e to your computer and use it in GitHub Desktop.
Save ryancraigmartin/1fa54832f62703e25a6359b0b8981d4e to your computer and use it in GitHub Desktop.
rename-files-in-pwd.go
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)
func renameFiles(root string) error {
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.Contains(info.Name(), "replacethis") {
newname := strings.Replace(info.Name(), "replacethis", "", -1)
newpath := filepath.Join(filepath.Dir(path), newname)
fmt.Printf("Renaming %s to %s\n", path, newpath)
return os.Rename(path, newpath)
}
return nil
})
return err
}
func main() {
root := "."
if err := renameFiles(root); err != nil {
fmt.Println("Error:", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment