Created
January 26, 2024 16:13
-
-
Save ryancraigmartin/1fa54832f62703e25a6359b0b8981d4e to your computer and use it in GitHub Desktop.
rename-files-in-pwd.go
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
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