问题描述
想要使用 csv 中列出的文件查找和替换字符串,遇到了障碍。以我目前所拥有的为例。任何帮助将不胜感激。
$inputfiles = Import-Csv D:\temp\testfolder\test.csv
$find = 'shazaam'
$replace ='shazaam1234'
foreach ($inputfile in $inputfiles) {
(Get-Content $PSItem.FullName) -Replace $oldString,$newString | Set-Content -Path
$PSItem.FullName
}
解决方法
如果你的 csv 的第一行是“全名”,那么下面的脚本应该可以工作。
# Import files with import csv (if first line isn't 'FullName' use Get-Content instead)
$inputfiles = Import-Csv D:\temp\testfolder\test.csv
$find = 'shazaam'
$replace ='shazaam1234'
# iterate through inputfiles
$inputfiles.Foreach({
# use .Replace instead because it uses no regular expressions
(Get-Content -Path $_.FullName).Replace($find,$replace) | Set-Content -Path $_.FullName
})