2012-11-02 17 views
-1

我在PowerShell相對較新。有人可以用簡單的腳本幫助我嗎?這裏是腳本:更改其內容後保存項目在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 

當然最後的設置內容失敗。我試圖保存剛更改過的txt文件。

回答

2

我不喜歡這樣寫道:

Get-ChildItem -path c:\MyDir -filter *.txt -recurse | 
    Foreach-Object { 
        (gc $_.FullName) | % 
             { 
             $_ -replace 'TextToReplace1', '' ` 
               -replace 'TextToReplace2', '' 
             } | Set-Content -Path $_.fullname 
        } 
+0

感謝基督教。雖然我沒有驗證你的解決方案是否可行,但我確信它會以它的樣子。不幸的是,在我編寫我的腳本之前,我沒有看到它。你的也許會更有效率。 – coding4fun

-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              
} 
相關問題