如何使用PowerShell修改降序文件夹结构中的所有文件?

时间:2013-01-30 06:37:10

标签: powershell sharepoint-2010

我试图在降序文件夹结构中更新具有.xml文件扩展名的所有文件,但我的脚本失败(可能是由于缺乏知识)。任何帮助都会很棒:

#Example of syntax
#=======================
# seekAndReplace -FilePath "\\sitecollection\newroot\pages" -FileExtension 
# "*.xml" -OldAddress "http://oldroot/sites/blank/" -NewAddress "http://newroot/sites/blank"
#


function seekAndReplace {
Param(
[string]$FilePath,
[string]$FileExtension,
[string]$OldAddress,
[string]$NewAddress
)
$files = Get-ChildItem $FilePath -Filter $FileExtension
    foreach ($file in $files) {
        (Get-Content $file.fullname) |
        ForEach-Object {$_ -replace $OldAddress,$NewAddress} |
        Set-Content $file.fullname
    } #end foreach
} #end function

1 个答案:

答案 0 :(得分:4)

这有帮助吗?请注意Regex Escape method的使用。你需要它来逃避特殊字符。

Get-ChildItem $FilePath -Filter $FileExtension | Foreach-Object {
        (Get-Content $_.fullname) -replace [regex]::Escape($OldAddress),$NewAddress | Set-Content $_.fullname
}
相关问题