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"
2
  • Why not use the global search & replace in Visual Studio or any other text editor which can perform searches on files? Commented Apr 22, 2013 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 Commented Apr 22, 2013 at 15:08

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.