我正在尝试使用以下方法在文件中查找和替换。
Function Find-Replace ($FileFullpath, $FindString, $ReplacementString) {
Get-Content $FileFullpath |
Foreach-Object {$_ -replace $FindString, $ReplacementString } |
Set-Content $FileFullpath
}
Find-Replace "c:\program files (x86)\MyProj\web.config" $OldServiceName $NewServiceName但我总是会遇到错误。
Set-Content :该进程无法访问文件“”c:\program files (X86)\MyProj\web.config“”,因为另一个进程正在使用该文件。“
该文件不会在任何位置打开。我认为Get-content还没有发布这个文件。
为什么会发生这种情况?如何在同一文件中进行查找和替换而不出现问题?
发布于 2012-05-07 18:55:25
当文件打开时,你不能读写同一个文件,Get-Content会打开文件进行读操作,同时Set-Content会尝试写入。将Get-Conetnt调用放在括号中,它将打开文件,读取其内容并关闭它。
(Get-Content $FileFullpath) | ...https://stackoverflow.com/questions/10480673
复制相似问题