希望通过 csv 文件使用 PowerShell 进行查找和替换

问题描述

想要使用 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
})

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...