-1

我在powershell相对较新。有人可以帮我写一个简单的脚本吗?这是脚本:

Clear-Host #clear command window
Set-Location c:\MyDir
Get-ChildItem -include *.txt -recurse  | Get-Content  | Foreach-Object {
                                                            $_  -replace 'TextToReplace1', '' `
                                                                -replace 'TextToReplace2', ''
                                                           } | Set-Content -WhatIf

当然,最后一个 Set-Content 失败了。我正在尝试保存我刚刚更改的 txt 文件。

4

2 回答 2

2

我这样做:

Get-ChildItem -path c:\MyDir -filter *.txt -recurse | 
     Foreach-Object { 
                     (gc $_.FullName) | % 
                                        {
                                         $_  -replace 'TextToReplace1', '' `
                                              -replace 'TextToReplace2', ''
                                        } | Set-Content  -Path $_.fullname 
                    }
于 2012-11-02T19:28:49.190 回答
-1

这就是我最终编写脚本的方式:

Set-Location c:\MyDir
$ProjFiles = Get-ChildItem -include *.txt -recurse  

foreach ($file in $ProjFiles)
{
    $NewFile = Get-Content $file.PSPath  | Foreach-Object {
                                                            $_  -replace 'TextToReplace1', '' `
                                                                -replace 'TextToReplace2', '' ` 
                                                           } 
     Set-Content $file.PSPath $Newfile                                                     
}
于 2012-11-07T17:34:20.717 回答