【发布时间】:2018-12-18 20:40:02
【问题描述】:
我正在使用下面的 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
}
问题是脚本还会修改其中没有匹配文本的文件。我们如何忽略没有匹配文本的文件?
【问题讨论】:
-
是否有任何选项可以忽略一些匹配的文本。例如,该文件还包含诸如 c:/hola/hello.xml 之类的文件路径。我想包含一个正则表达式或条件,以便在 /hola/ 之间不更改 hola 或者如果它是 hello.xml 的文件名并更改其他出现。