Skip to content

Instantly share code, notes, and snippets.

@Katsaros
Last active September 22, 2021 13:30
Show Gist options
  • Select an option

  • Save Katsaros/4df7bc078a3e5227ad7e3fac7d03bb8b to your computer and use it in GitHub Desktop.

Select an option

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
$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