rm -f 相当于 PowerShell 忽略不存在的文件

问题描述 投票:0回答:3

背景

我有一个 PowerShell 脚本,可以将一些结果写入文件中。

  • 我想在脚本开始时使用
    Remove-Item
    自动删除结果文件。
  • 您可以手动删除结果文件,因此即使结果文件不存在,我也不想显示错误消息。
  • 我想在脚本因其他原因无法删除结果文件时显示错误消息,例如文件已锁定。

您可以在类 Unix 系统中使用

rm -f
满足上述所有要求。

问题

首先我尝试过

Remove-Item -Force
,但它无法忽略不存在的文件(参见
rm -f
忽略不存在的文件)。

PS C:\tmp> Remove-Item C:\tmp\foo.txt -Force
Remove-Item : Cannot find path 'C:\tmp\foo.txt' because it does not exist.
At line:1 char:1
+ Remove-Item C:\tmp\foo.txt -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\tmp\foo.txt:String) [Remove-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

接下来,我尝试了

Remove-Item -ErrorAction Ignore
Remove-Item -ErrorAction SilentlyContinue
,但它们在删除文件失败时不显示错误消息(参见
rm -f
在这种情况下显示类似
rm: cannot remove 'foo.txt': Operation not permitted
的错误消息)。

PS C:\tmp> $file = [System.IO.File]::Open('C:\tmp\foo.txt',[System.IO.FileMode]::Open,[System.IO.FileAccess]::Read,[System.IO.FileShare]::None)
PS C:\tmp> Remove-Item C:\tmp\foo.txt -ErrorAction Ignore
# I expected it shows an error because it couldn't remove the file because of the lock, but it showed nothing
PS C:\tmp> $file = [System.IO.File]::Open('C:\tmp\foo.txt',[System.IO.FileMode]::Open,[System.IO.FileAccess]::Read,[System.IO.FileShare]::None)
PS C:\tmp> Remove-Item C:\tmp\foo.txt -ErrorAction SilentlyContinue
# I expected it shows an error because it couldn't remove the file because of the lock, but it showed nothing

问题

PowerShell 中是否有

rm -f
等效项可以满足上述所有要求?

powershell rm
3个回答
17
投票

对我来说,最简单的解决方案是:

if (test-path $file) {
  remove-item $file
}

我也有这样的想法。 $error[0] 始终是最近的错误。

remove-item $file -erroraction silentlycontinue
if ($error[0] -notmatch 'does not exist') {
  write-error $error[0]  # to standard error
}

我认为你也可以使用 try/catch 来处理特定的异常。这是一个例子。我通过制表符补全发现了异常。但脚本将因其他未捕获的异常而停止。此错误通常不会停止。

try { remove-item foo -erroraction stop }
catch [System.Management.Automation.ItemNotFoundException] { $null }
'hi'

1
投票

您无法单独使用该 cmdlet 来执行此操作。您必须为错误提供额外的逻辑。

'D:\temp\abc.txt', 'D:\Temp\hw.txt', 'D:\Temp\nonexistent.txt.', 'D:\Temp\book1.csv' | 
ForEach{
    try   {Remove-Item   -Path    $PSitem -WhatIf -ErrorAction Stop}
    catch {Write-Warning -Message $PSItem.Exception.Message}
}
# Results
<#
What if: Performing the operation "Remove File" on target "D:\temp\abc.txt".
What if: Performing the operation "Remove File" on target "D:\Temp\hw.txt".
WARNING: Cannot find path 'D:\Temp\nonexistent.txt.' because it does not exist.
What if: Performing the operation "Remove File" on target "D:\Temp\book1.csv".
#>

您应该在所有代码(交互式代码和脚本)中利用错误处理。 您可以将任何屏幕输出发送到 $null、Out-Null 或 [void] 以防止其进入屏幕,但仍然知道它发生了。

对于您要求的用例,您将需要多个逻辑(try/catch、if/then)语句。

所以,像这样修改过的包装函数:

function Remove-ItemNotFileLocked
{
    [cmdletbinding(SupportsShouldProcess)]
    [Alias('rinf')]

    param 
    (
        [parameter(Mandatory = $true)][string]$FullFilePath
    )

    $TargetFile = New-Object System.IO.FileInfo $FullFilePath

    if ((Test-Path -Path $FullFilePath) -eq $false) {return $false}

    try 
    {
        $TargetFileStream = $TargetFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)

        if ($TargetFileStream) 
        {
            $TargetFileStream.Close()
            Remove-Item -Path $FullFilePath
        }
        $false
    } 
    catch 
    {
        $true
    }
}

'D:\temp\abc.txt', 'D:\Documents\Return To Sender.docx','D:\Temp\nonexistent.txt.' | 
ForEach {Remove-ItemNotFileLocked -FullFilePath $PSItem -WhatIf}

# Results
<#
What if: Performing the operation "Remove File" on target "D:\temp\abc.txt".
True
False
#>

注意点:记事本等文本编辑器不会对文件加锁。

  • 第一条消息显示记事本文件已打开,但可以删除
  • 第二个显示 Word 文档已打开并锁定
  • 第三个显示文本文件,不在系统中

如果我不想要屏幕噪音,那么...... 注释掉那些 $false 和 $True 语句,它们用于调试和验证工作。

'D:\temp\abc.txt', 'D:\Documents\Return To Sender.docx','D:\Temp\nonexistent.txt.' | 
ForEach {$null = Remove-ItemNotFileLocked -FullFilePath $PSItem -WhatIf}
# Results
<#
What if: Performing the operation "Remove File" on target "D:\temp\abc.txt".
#>

当然,删除/注释掉 -WhatIf 让事情发生,噪音也会消失。

如果您不想使用函数,那么此代码块应该可以解决您的用例。

# Remove non-Locked file and show screen output
'D:\temp\abc.txt', 'D:\Documents\Return To Sender.docx','D:\Temp\nonexistent.txt.' | 
ForEach{
    try   
    {
        $TargetFile = (New-Object System.IO.FileInfo $PSitem).Open(
                                            [System.IO.FileMode]::Open, 
                                            [System.IO.FileAccess]::ReadWrite, 
                                            [System.IO.FileShare]::None
                      )
        $TargetFile.Close()  
        Remove-Item -Path $PSItem -WhatIf  
    }
    catch [System.Management.Automation.ItemNotFoundException]{$PSItem.Exception.Message}
    catch {$PSItem.Exception.Message}
}

# Results
<#
What if: Performing the operation "Remove File" on target "D:\temp\abc.txt".
Exception calling "Open" with "3" argument(s): "The process cannot access the file 'D:\Documents\Return To Sender.docx' because it is being used by another process."
Exception calling "Open" with "3" argument(s): "Could not find file 'D:\Temp\nonexistent.txt'."
#>

# Remove non-Locked file and silence screen output
'D:\temp\abc.txt', 'D:\Documents\Return To Sender.docx','D:\Temp\nonexistent.txt.' | 
ForEach{
    try   
    {
        $TargetFile = (New-Object System.IO.FileInfo $PSitem).Open(
                                            [System.IO.FileMode]::Open, 
                                            [System.IO.FileAccess]::ReadWrite, 
                                            [System.IO.FileShare]::None
                      )
        $TargetFile.Close()  
        Remove-Item -Path $PSItem -WhatIf 
    }
    catch [System.Management.Automation.ItemNotFoundException]{$null = $PSItem.Exception.Message}
    catch {$null = $PSItem.Exception.Message}
}
# Results
<#
What if: Performing the operation "Remove File" on target "D:\temp\abc.txt".
#>

0
投票

我们需要使用

Test-Path $item
来验证
$item
是否存在。

  • $item
    不存在时,
    Remove-Item $item
    将以错误结束。

所以下面的

quiet_rm()
函数是为了模拟linux
rm -rf file/folder
命令而设计的。

  • 即使
    $item
    不存在,也不会报告任何错误消息。
# Remove a file or folder quietly
# Like linux "rm -rf"
function quiet_rm($item)
{
  if (Test-Path $item) {
    echo "  Removing $item"
    Remove-Item $item  -r -force
  }
}

echo "Clear folder and files"
quiet_rm .\build\
quiet_rm   build.gradle
quiet_rm  .gradle
quiet_rm   gradle
quiet_rm   gradle.properties
© www.soinside.com 2019 - 2024. All rights reserved.