删除断开的链接 [英] Delete broken link

查看:218
本文介绍了删除断开的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要删除文件夹的所有内容,其中可能包含断开的链接等。文件夹路径由变量提供。问题是 PowerShell 无法删除损坏的链接。

I need to delete all the content of a folder which may include broken links among others. The folder path is provided by a variable. Problem is that PowerShell fails to remove the broken links.

$folderPath = "C:\folder\"

尝试1:

Remove-Item -Force -Recurse -Path $folderPath

失败并显示错误:

Remove-Item : Could not find a part of the path 'C:\folder\brokenLink'.
At line:1 char:1
+ Remove-Item -Force -Recurse -Path $folderPath
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\folder\brokenLink:String) [Remove-Item], DirectoryNot 
   FoundException
    + FullyQualifiedErrorId : RemoveItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

尝试2:

Start-Job { cmd /c rmdir $folderPath }

失败,因为 $ folderPath 按原样而不是其值传递:

Fails because $folderPath gets passed as is instead of its value:

Start-Job { cmd /c rmdir $folderPath } | select command

Command                                                                                             
-------                                                                                             
 cmd /c rmdir $folderPath                                                                           

除了使用 .NET 框架之外还有任何建议吗?

Any suggestion besides using the .NET framework?

编辑

通过断开的链接,我指的是一个指向先前安装的分区的文件夹,它不再存在。该文件夹仍然可用但在尝试导航时出现此错误,因为目标不再存在:

错误:

EDIT
By broken link I'm referring to a folder which points to a previously mounted partition, that doesn't exist anymore. The folder is still available but when attempting to navigate into it this error occurs because the destination doesn't exist anymore:
Error:


C:\ folder @\\brokenLink是指不可用的位置。
可以在这台计算机上的硬盘上,也可以在网络上。检查
确保磁盘已正确插入,或者您是
连接到Internet或网络,然后重试。如果仍无法找到
,则该信息可能已移至
不同位置。

C:\folder\brokenLink refers to a location that is unavailable. It could be on a hard drive on this computer, or on a network. Check to make sure that the disk is properly inserted, or that you are connected to the Internet or your network, and then try again. If it still cannot be located, the information might have been moved to a different location.


推荐答案

这将有效:

$folderPath = "C:\folderContaingBrokenSymlinks\";
$items = ls $folderPath -Recurse -ea 0;
foreach($item in $items){
    if($item.Attributes.ToString().contains("ReparsePoint")){
        cmd /c rmdir $item.PSPath.replace("Microsoft.PowerShell.Core\FileSystem::","");
    }
    else{
        rm -Force -Recurse $item;
    }
}

这篇关于删除断开的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆