我在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文件。
答案 0 :(得分:2)
我这样做:
Get-ChildItem -path c:\MyDir -filter *.txt -recurse |
Foreach-Object {
(gc $_.FullName) | %
{
$_ -replace 'TextToReplace1', '' `
-replace 'TextToReplace2', ''
} | Set-Content -Path $_.fullname
}
答案 1 :(得分:-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
}