【发布时间】:2013-04-23 00:15:08
【问题描述】:
到目前为止,我正在尝试将所有 .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"
【问题讨论】:
-
为什么不在 Visual Studio 或任何其他可以对文件执行搜索的文本编辑器中使用全局搜索和替换?
-
有些文件我不想搜索和替换,全局会做出改变,另外我想制作一个脚本。这本来是一个简单的替代品,所以如果你想把它作为答案,你会得到我的支持,但 Atique 的帖子正是我要找的
标签: powershell