更改项目内容后是否在powershell中保存项目?
我是powershell的新手。有人能帮我写一个简单的脚本吗?以下是脚本:更改项目内容后是否在powershell中保存项目?,powershell,Powershell,我是powershell的新手。有人能帮我写一个简单的脚本吗?以下是脚本: Clear-Host #clear command window Set-Location c:\MyDir Get-ChildItem -include *.txt -recurse | Get-Content | Foreach-Object { $_ -replace 'TextToRepl
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-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
}
当然,最后一组内容失败了。我正在尝试保存刚刚更改的txt文件。我是这样做的:
Get-ChildItem -path c:\MyDir -filter *.txt -recurse |
Foreach-Object {
(gc $_.FullName) | %
{
$_ -replace 'TextToReplace1', '' `
-replace 'TextToReplace2', ''
} | Set-Content -Path $_.fullname
}
我就是这样写剧本的:
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-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
}
谢谢你,克里斯蒂安。虽然我没有验证你的解决方案是否有效,但从外观上看,我确信它会有效。不幸的是,我在写剧本之前没有看到它。你的可能也会更有效率。