Get-ChildItem error handling when using long file paths

后端 未结 2 1631
鱼传尺愫
鱼传尺愫 2020-12-12 03:17

I am trying to handle errors when scanning through folders. Let\'s say I have something like:

Get-ChildItem $somepath -Directory | ForEach-Object {
   if(err         


        
相关标签:
2条回答
  • 2020-12-12 03:18

    If you can install a non-ancient PowerShell version (3.0 or newer), simply prepend the path with \\?\ to overcome the 260-character limit for full path:

    Get-ChildItem "\\?\$somepath" | ForEach {
        # ............
    }
    
    0 讨论(0)
  • 2020-12-12 03:19

    You could try ignoring the files longer 260 characters by using the Where-Object cmdlet.

    Get-ChildItem $somepath -Directory -ErrorAction SilentlyContinue `
        | Where-Object {$_.length -lt 261} `
        | ForEach-Object { Write-Host $_.BaseName }
    

    Or you could use the following (Ref).

    cmd /c dir $somepath /s /b | Where-Object {$_.length -lt 261} 
    
    0 讨论(0)
提交回复
热议问题