【问题标题】:Could not find a part of the path Exception Thrown找不到路径的一部分抛出异常
【发布时间】:2013-02-19 15:08:06
【问题描述】:
$ErrorActionPreference = 'Continue'

try
{
    $fileEntries = [IO.Directory]::GetFiles("somedirectory", "*",[IO.SearchOption]::AllDirectories); 
}
catch [Exception]
{
    $_.Exception.Message
}
foreach($fileName in $fileEntries) 
{ 
    [Console]::WriteLine($fileName); 
} 

以上代码抛出异常

找不到路径 '\blah\dah\dodah\updity_and_suppy\03 的一部分。文档 hokeypokey\imp\lalalid\welpy qwerty-poloky 架构 - tada sausy(来自互联网) todady poloky TPG upodsl poliduity\B4PE to Y3 - E xternal asdfds polm toluky to Saftgy 3 yuppy App\Old Docs'。

我稍微混淆了路径,但布局在那里。

我尝试使用 GetFiles 而不是 Get-ChildItem 的原因是速度。我正在通过网络遍历可能有 100 TB 的数据,而 Get-ChildItem 在检索 .Net 对象时速度太慢,并且包含的​​信息超出了我的需要。

我要问的问题是“可以使用类似于 > -ErrorAction SilentlyContinue 的方法忽略此错误吗?”或“有没有办法阻止此错误的发生?”

感谢您的考虑。

【问题讨论】:

  • 请提供完整的例外情况。提供的文件路径是绝对的还是实际上更长?它非常接近 Windows 的最大路径限制(长度)
  • @Graimer 这是我收到的完整异常减去出于安全原因被混淆(屏蔽)的路径。处理后时间更短。我意识到超过 260 个字符的任何内容都超过了 Max_Path。我想知道是否有办法处理这个错误并继续(Get-ChildItem 这样做但太慢了)还是有另一种语言更适合?

标签: .net powershell


【解决方案1】:
$ErrorActionPreference = 'Stop'

function recurse
{

    param($path)

    try
    {
           $fileEntries = [IO.Directory]::GetFiles($path)
           [System.IO.DirectoryInfo]$directInf = New-Object IO.DirectoryInfo($path)
           $folders = $directInf.GetDirectories()
    }
    catch [Exception]
    {
           $_.Exception.Message
           $folders = @()
     }

    foreach($fileName in $fileEntries) 
    {

          #[Console]::WriteLine($fileName); 

    }

    Remove-Variable -ErrorAction SilentlyContinue -Name fileEntries 

    foreach($folder in $folders)
    {
        recurse($path + $folder + "\")
    }
    Remove-Variable -ErrorAction SilentlyContinue -Name folders 
}

recurse #path that you wish to search

这可能不是最好的解决方案,但它会递归地遍历文件夹并获取文件名,比网络上的 get-childitem 快得多,并且处理超过 260 的路径引发的异常并继续(它不解决问题,只是忽略并继续)。 Remove-Variable 是帮助解决内存问题,有没有帮助是另一回事。

【讨论】:

    猜你喜欢
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 2017-12-19
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    相关资源
    最近更新 更多