当前位置:  开发笔记 > 编程语言 > 正文

Powershell - 跳过无法访问的文件

如何解决《Powershell-跳过无法访问的文件》经验,为你挑选了1个好方法。

我试图用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

Remove-Item:无法删除项目C:\ Users\mstjean\Desktop\The_folder\Delete:进程无法访问文件'C:\ Users\mstjean\Desktop\The_folder\Delete',因为它正由另一个进程使用.在行:1 char: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

但我得到了这个错误.



1> Bacon Bits..:

如果要禁止显示错误消息并继续执行,则需要使用-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块是个坏主意,所以谨慎使用这个方法!

你会注意到我实际上并没有测试文件是否被打开或锁定.那是因为这是浪费时间.我们并不真正关心为什么文件无法删除,只是它不能删除,什么时候不能删除它.尝试删除文件并捕获故障比检查它是否被锁定更容易(也更快),使用条件来决定删除文件,然后删除文件或继续.

推荐阅读
wangtao
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有