Powershell - 跳过无法访问的文件

时间:2015-12-14 19:10:06

标签: powershell

我尝试使用Powershell递归删除文件夹内的所有文件和文件夹。我试着用一个简单的工作:

Remove-Item "C:\Users\user\Desktop\The_folder\*" -Recurse -Force

我的问题是每当我运行它时,我得到:

Cannot remove item C:\Users\user\Desktop\The_folder\Delete: The process cannot access the file 'C:\Users\user\Desktop\The_folder\Delete' because it is being used by another process.

如何跳过我无法访问的任何文件(可能是因为它们正在使用中)?用户在弹出窗口中执行的方式与询问是否要跳过无法访问的所有文件的方式相同。

谢谢!

编辑: 我试过了:

Remove-Item "C:\Users\mstjean\Desktop\The_folder\*" -Recurse -Force -ErrorAction Continue
  

删除项目:无法删除项目   C:\ Users \ mstjean \ Desktop \ The_folder \ Delete:进程无法访问   文件' C:\ Users \ mstjean \ Desktop \ The_folder \ Delete'因为它是   被另一个进程使用。在行:1个字符:1   + Remove-Item" C:\ Users \ mstjean \ Desktop \ The_folder *" -Recurse -Force -ErrorAction ...   +

     

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~       + CategoryInfo:WriteError:(C:\ Users \ mstjea ... e_folder \ Delete:DirectoryInfo)[Remove-Item],   IOException异常       + FullyQualifiedErrorId:RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

但我收到了这个错误。

2 个答案:

答案 0 :(得分:21)

如果要取消错误消息并继续执行,则需要使用-ErrorAction Ignore-ErrorAction SilentlyContinue

请参阅Get-Help about_CommonParameters

 -ErrorAction[:{Continue | Ignore | Inquire | SilentlyContinue | Stop |
     Suspend }]
     Alias: ea

     Determines how the cmdlet responds to a non-terminating error
     from the command. This parameter works only when the command generates
     a non-terminating error, such as those from the Write-Error cmdlet.

     The ErrorAction parameter overrides the value of the
     $ErrorActionPreference variable for the current command.
     Because the default value of the $ErrorActionPreference variable
     is Continue, error messages are displayed and execution continues
     unless you use the ErrorAction parameter.

     The ErrorAction parameter has no effect on terminating errors (such as
     missing data, parameters that are not valid, or insufficient
     permissions) that prevent a command from completing successfully.

     Valid values:

         Continue. Displays the error message and continues executing
         the command. "Continue" is the default value.

         Ignore.  Suppresses the error message and continues
         executing the command. Unlike SilentlyContinue, Ignore
         does not add the error message to the $Error automatic
         variable. The Ignore value is introduced in Windows
         PowerShell 3.0.

         Inquire. Displays the error message and prompts you for
         confirmation before continuing execution. This value is rarely
         used.

         SilentlyContinue. Suppresses the error message and continues
         executing the command.

         Stop. Displays the error message and stops executing the
         command.

         Suspend. This value is only available in Windows PowerShell workflows.
         When a workflow runs into terminating error, this action preference
         automatically suspends the job to allow for further investigation. After
         investigation, the workflow can be resumed.

如果您遇到-ErrorAction没有陷阱的终止错误,那么您必须自己使用try / catch捕获它们。

这是一个天真的例子:

Get-ChildItem "C:\Users\user\Desktop\The_folder\*" -Recurse -Force `
| Sort-Object -Property FullName -Descending `
| ForEach-Object {
    try {
        Remove-Item -Path $_.FullName -Force -ErrorAction Stop;
    }
    catch { }
}

在这里,我使用-ErrorAction Stop将所有非终止错误转换为终止错误。 Try将捕获任何终止错误。但是,catch块为空,因此您将捕获所有内容,然后不进行任何错误处理。该脚本将继续静默。这基本上等同于VBScript的On Error Resume Next。但是,您必须遍历文件,否则Remove-Item将在第一个错误处停止。

请注意,我有一个Sort-Object。这就是通过管道的物品顺序相反。这样,文件和子目录将在包含它们的目录之前被删除。我不是100%确定这种方法是否完美,但我认为它应该有效。另一种选择非常麻烦。

显然,当错误发生或未删除时,无法从输出中判断。我们正在捕获所有错误,然后扔掉它们。 通常一个空的catch块是一个非常糟糕的主意,所以请谨慎使用此方法!

你会注意到我实际上并没有测试是否打开或锁定了文件。那是因为这是浪费时间。我们不真的关心为什么文件无法删除,只是它不能删除,什么时候不能删除它。尝试删除文件并捕获失败比check if it's locked更容易(也更快),使用条件来决定删除文件,然后删除文件或继续。

答案 1 :(得分:0)

添加-ea静默继续,一切正常