我正在使用下面的PowerShell脚本来搜索和替换,它能正常工作。
问题在于脚本还修改了那些没有匹配文本的文件。我们如何忽略那些没有匹配文本的文件呢?
$files = Get-ChildItem 'E:\replacetest' -Include "*.txt" -Recurse | ? {Test-Path $_.FullName -PathType Leaf}
foreach($file in $files)
{
$content = Get-Content $file.FullName | Out-String
$content| Foreach-Object{$_ -replace 'hello' , 'hellonew'`
-replace 'hola' , 'hellonew' } | Out-File $file.FullName -Encoding utf8
}
问题在于脚本还修改了那些没有匹配文本的文件。我们如何忽略那些没有匹配文本的文件呢?