Last active
September 22, 2021 13:30
-
-
Save Katsaros/4df7bc078a3e5227ad7e3fac7d03bb8b to your computer and use it in GitHub Desktop.
Replace names and texts in multiple directories and files. Run it using PowerShell, with command .\Renamator.ps1
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
| $files = Get-ChildItem "C:\Users\Me\Desktop\directoryname*" -Recurse | |
| $find = 'text-to-change' | |
| $replace = 'with-this-one' | |
| $textChanges = 0 | |
| $fileAndFolderNameChanges = 0 | |
| write-host "`n [INFO][Starting the process to change the text"$find" to "$replace" inside the files]" | |
| Get-ChildItem $files -Recurse | | |
| select -ExpandProperty fullname | | |
| foreach { | |
| If(Select-String -Path $_ -SimpleMatch $find -quiet){ | |
| (Get-Content $_) | | |
| ForEach-Object {$_ -replace $find, $replace } | | |
| Set-Content $_ | |
| write-host "Discovered text inside the file and changed : " $_ | |
| $textChanges++ | |
| } | |
| } | |
| write-host "`n [INFO][Starting the process to change the file names from "$find" to "$replace"]" | |
| forEach($File in $files) | |
| { | |
| $newname = ([String]$File).Replace($find,$replace) | |
| if ([String]$File -ne $newname) { | |
| write-host "Renaming $File to $newname" | |
| Rename-item -Path $File.PSPath $newname | |
| $fileAndFolderNameChanges++ | |
| } | |
| } | |
| write-host "`n [DONE][Changed texts:"$textChanges" | Changed folder names: "$fileAndFolderNameChanges |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment