1

I'm trying to replace CodeFile to CodeBehind in all of my .aspx / aspx.cs files so far I have

$FileLocation = "F:\Code\trunk\Source\"

$FindAllItems = Get-ChildItem $FileLocation -Rec -Inc *.aspx,*.aspx.cs 

foreach ($file in $FindAllItems)
{

  foreach($line in @(Get-Content $file))
   {       
     if ($line -match  "CodeFile")
     {  
        Write-Host $line
        Write-Host "Renaming CodeFile to CodeBehind"
        $replacedLine = $line.Replace("CodeFile", "CodeBehind")
        Write-Host "edited line:" $replacedLine
        Write-Host "Saving change, naming" $file.fullname
     }         

   }    
}
Write-Host "Done"

how could i use the .save parameter in this situation or what is the best way to commit the edit?

Update:

After being told about Set-Content, I re-wrote the script, below is the working code.

$FileLocation = "F:\Code\trunk\Source\"

$FindAllItems = Get-ChildItem $FileLocation -Rec -Inc *ascx,*ascx.cs

foreach ($file in $FindAllItems)
{

(Get-Content $file) | 
Foreach-Object {$_ -replace "CodeFile", "CodeBehind"} | 
Set-Content $file

}
Write-Host "Done"
Lewis
  • 2,373
  • 2
  • 22
  • 30
  • Why not use the global search & replace in Visual Studio or any other text editor which can perform searches on files? – alroc Apr 22 '13 at 13:06
  • There are certain files I don't want to search & replace, global would make the change, plus I wanted to make a script. This would have been an easy substitute, so if you want to put it up as an answer, you'd get an upvote from me, but Atique's post was what I was looking for – Lewis Apr 22 '13 at 15:08

1 Answers1

2

Possible duplicate of 1 & 2. You can use Set-Content Cmdlet to commit changes. See example and explanation here.

Community
  • 1
  • 1
Atiq Rahman
  • 680
  • 6
  • 24