【问题标题】:Delete broken link删除断开的链接
【发布时间】:2014-02-15 21:33:30
【问题描述】:

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

$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 按原样而不是其值传递:

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

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

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

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

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

【问题讨论】:

  • 你能澄清一下“断开的链接”是什么意思吗?你有一个装满快捷方式文件的文件夹,还是什么?
  • @TrevorSullivan 我已经用我的解释编辑了这个问题。

标签: powershell hyperlink


【解决方案1】:

这将起作用:

$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;
    }
}

【讨论】:

  • 你能解释一下而不是仅仅粘贴sn-p吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-21
  • 2023-01-30
  • 1970-01-01
相关资源
最近更新 更多