Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更改项目内容后是否在powershell中保存项目?_Powershell - Fatal编程技术网

更改项目内容后是否在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

我是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-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                                                     
}

谢谢你,克里斯蒂安。虽然我没有验证你的解决方案是否有效,但从外观上看,我确信它会有效。不幸的是,我在写剧本之前没有看到它。你的可能也会更有效率。