到目前为止,我正在尝试将所有 .aspx / aspx.cs 文件中的 CodeFile 替换为 CodeBehind
$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"
在这种情况下如何使用 .save 参数或者提交编辑的最佳方式是什么?
更新:
在被告知 Set-Content 后,我重新编写了脚本,下面是工作代码。
$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"