11# ##############################################################################
22<#
33. SYNOPSIS
4- Performs case-sensitive text replacement throughout a project directory.
4+ Performs text replacement throughout a project directory.
55
66. DESCRIPTION
77Recursively searches through files and directories in a project to perform text
88replacements. Handles both file/directory names and file contents. Skips common
99binary files and repository folders (.git, .svn) to avoid corruption. Uses UTF-8
10- encoding without BOM for file operations.
10+ encoding without BOM for file operations. Supports both case-sensitive and
11+ case-insensitive replacement modes.
1112
1213. PARAMETER Source
1314The directory, filepath, or directory+searchmask to process. Defaults to current
1415directory if not specified.
1516
1617. PARAMETER FindText
17- The case-sensitive text pattern to search for in filenames and content.
18+ The text pattern to search for in filenames and content. Case sensitivity is
19+ controlled by the CaseInsensitive parameter.
1820
1921. PARAMETER ReplacementText
2022The text to replace all instances of FindText with.
2123
24+ . PARAMETER CaseInsensitive
25+ Perform case-insensitive text replacement. When specified, matching is done
26+ without regard to case.
27+
2228. PARAMETER WhatIf
2329Shows what changes would occur without actually making them.
2430
@@ -28,6 +34,9 @@ Rename-InProject -Source .\src\*.js -FindText "oldName" `
2834
2935. EXAMPLE
3036rip . "MyClass" "MyNewClass" -WhatIf
37+
38+ . EXAMPLE
39+ rip . "OLDNAME" "NewName" -CaseInsensitive
3140#>
3241function Rename-InProject {
3342
@@ -49,7 +58,7 @@ function Rename-InProject {
4958 Mandatory = $true ,
5059 Position = 1 ,
5160 ValueFromPipeline = $false ,
52- HelpMessage = ' The text to find (case sensitive )'
61+ HelpMessage = ' The text to find (case sensitivity controlled by CaseInsensitive parameter )'
5362 )]
5463 [Alias (' find' , ' what' , ' from' )]
5564 [ValidateNotNullOrEmpty ()]
@@ -63,7 +72,14 @@ function Rename-InProject {
6372 )]
6473 [Alias (' into' , ' txt' , ' to' )]
6574 [ValidateNotNull ()]
66- [string ] $ReplacementText
75+ [string ] $ReplacementText ,
76+ # #######################################################################
77+ [Parameter (
78+ Mandatory = $false ,
79+ ValueFromPipeline = $false ,
80+ HelpMessage = ' Perform case-insensitive text replacement'
81+ )]
82+ [switch ] $CaseInsensitive
6783 # #######################################################################
6884 )
6985
@@ -161,7 +177,14 @@ function Rename-InProject {
161177 # replace text in file contents
162178 $content = [IO.File ]::ReadAllText($filePath ,
163179 [Text.Encoding ]::UTF8)
164- $newContent = $content.Replace ($FindText , $ReplacementText )
180+
181+ if ($CaseInsensitive ) {
182+ $newContent = $content.Replace ($FindText , $ReplacementText ,
183+ [StringComparison ]::OrdinalIgnoreCase)
184+ }
185+ else {
186+ $newContent = $content.Replace ($FindText , $ReplacementText )
187+ }
165188
166189 if ($content -ne $newContent ) {
167190 if ($PSCmdlet.ShouldProcess ($filePath ,
@@ -181,7 +204,13 @@ function Rename-InProject {
181204
182205 # handle filename changes
183206 $oldName = [IO.Path ]::GetFileName($filePath )
184- $newName = $oldName.Replace ($FindText , $ReplacementText )
207+ if ($CaseInsensitive ) {
208+ $newName = $oldName.Replace ($FindText , $ReplacementText ,
209+ [StringComparison ]::OrdinalIgnoreCase)
210+ }
211+ else {
212+ $newName = $oldName.Replace ($FindText , $ReplacementText )
213+ }
185214
186215 if ($oldName -ne $newName ) {
187216 $newPath = [IO.Path ]::Combine(
@@ -223,7 +252,13 @@ function Rename-InProject {
223252
224253 $dir = $_
225254 $oldName = $dir.Name
226- $newName = $oldName.Replace ($FindText , $ReplacementText )
255+ if ($CaseInsensitive ) {
256+ $newName = $oldName.Replace ($FindText , $ReplacementText ,
257+ [StringComparison ]::OrdinalIgnoreCase)
258+ }
259+ else {
260+ $newName = $oldName.Replace ($FindText , $ReplacementText )
261+ }
227262
228263 if ($oldName -ne $newName ) {
229264 $newPath = GenXdev.FileSystem\Expand-Path (
0 commit comments