PowerShell的:有问题以“文件已在使用”设置内容(PowerShell: Set-Conte

2019-09-16 15:48发布

我工作的一个PowerShell脚本,找到与给定目录内模式的所有文件,打印出的文档的相关行与该模式强调的,然后用提供REPLACE字替换的模式,然后保存该文件回来。 所以它实际上编辑文件。

除了我不能让它改变文件,因为Windows抱怨该文件已被打开。 我尝试了几种方法来解决这个问题,但继续运行到这个问题。 也许有人可以帮助:

param(
    [string] $pattern = ""
    ,[string] $replace = ""
    ,[string] $directory ="."
    ,[switch] $recurse = $false
    ,[switch] $caseSensitive = $false)

if($pattern -eq $null -or $pattern -eq "")
{
    Write-Error "Please provide a search pattern." ; return
}

if($directory -eq $null -or $directory -eq "")
{
    Write-Error "Please provide a directory." ; return
}

if($replace -eq $null -or $replace -eq "")
{
    Write-Error "Please provide a string to replace." ; return
}

$regexPattern = $pattern
if($caseSensitive -eq $false) { $regexPattern = "(?i)$regexPattern" }
$regex = New-Object System.Text.RegularExpressions.Regex $regexPattern

function Write-HostAndHighlightPattern([string] $inputText)
{
    $index = 0
    $length = $inputText.Length
    while($index -lt $length)
    {
        $match = $regex.Match($inputText, $index)
        if($match.Success -and $match.Length -gt 0)
        {
            Write-Host $inputText.SubString($index, $match.Index) -nonewline
            Write-Host $match.Value.ToString() -ForegroundColor Red -nonewline
            $index = $match.Index + $match.Length
        }
        else
        {
            Write-Host $inputText.SubString($index) -nonewline
            $index = $inputText.Length
        }
    }
}

Get-ChildItem $directory -recurse:$recurse |
    Select-String -caseSensitive:$caseSensitive -pattern:$pattern |    
    foreach {
        $file = ($directory + $_.FileName)
        Write-Host "$($_.FileName)($($_.LineNumber)): " -nonewline
        Write-HostAndHighlightPattern $_.Line
        %{ Set-Content $file ((Get-Content $file) -replace ([Regex]::Escape("[$pattern]")),"[$replace]")}
        Write-Host "`n"
        Write-Host "Processed: $($file)"
    }

问题所在的代码最终块内,就在GET-ChildItem通话。 当然,有些该块中的代码是现在有点错位,由于我试图解决这个问题,然后停止,但请记住,脚本的那部分的意图。 我想要得到的内容,更换的话,然后保存修改的内容回我是从该文件。

任何帮助都将不胜感激。

Answer 1:

打消了我以前的答案,这个替换它:

Get-ChildItem $directory -recurse:$recurse
foreach {        
    $file = ($directory + $_.FileName)

    (Get-Content $file) | Foreach-object {
        $_ -replace ([Regex]::Escape("[$pattern]")),"[$replace]")
    } | Set-Content $file
}

注意:

  • 括号围绕Get-Content ,以确保文件被一次咕噜咕噜(并因此关闭)。
  • 管道到后续命令,而不是内联。
  • 一些您的命令已被删除,以确保它是一个简单的测试。


Answer 2:

只是一个建议,但你可以尝试寻找的参数代码块中的文档。 还有就是要确保,如果你需要它,如果用户没有抛出错误信息输入的参数更有效的方式。

About_throw: http://technet.microsoft.com/en-us/library/dd819510.aspx About_functions_advanced_pa​​rameters: http://technet.microsoft.com/en-us/library/dd347600.aspx

然后关于使用写入主机的所有时间: http://powershell.com/cs/blogs/donjones/archive/2012/04/06/2012-scripting-games-commentary-stop-using-write-host.aspx



Answer 3:

好吧,我终于坐了下来,刚才输入的一切相继在PowerShell中,然后用,为了使我的脚本。

它实际上是非常简单的;

$items = Get-ChildItem $directory -recurse:$recurse
$items |
foreach {
    $file = $_.FullName
    $content = get-content $file
    $newContent = $content -replace $pattern, $replace
    Set-Content $file $newcontent
}

感谢您的所有帮助球员。



文章来源: PowerShell: Set-Content having issues with “file already in use”