45293

Get-ChildItem error handling when using long file paths

Question:

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

Get-ChildItem $somepath -Directory | ForEach-Object { if(error occurred due to too long path) { skip this folder then } else { Write-Host $_.BaseName } }

When I do this I print the folders in $somepath until one of them is too long and then the loop stops. Even when using SilentlyContinue. I want to print even after reaching a folder that is too long.

Answer1:

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 { # ............ }

Answer2:

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 (<a href="https://stackoverflow.com/questions/12697259/how-do-i-find-files-with-a-path-length-greater-than-260-characters-in-windows" rel="nofollow">Ref</a>).

cmd /c dir $somepath /s /b | Where-Object {$_.length -lt 261}

Recommend

  • Azure Powershell - across MULTIPLE subscriptions in an EA
  • Resolve-DnsName inside Test-Connection
  • VSTS docker task failed on '&&' token in docker RUN command
  • PowerShell Start-Service Timeout
  • react-helmet outputting empty strings on server-side
  • Zipping the contents of a folders in PHP
  • Linear gradient not applying in Webkit with d3 generated SVG
  • Add class element based on the name of the page
  • How to Divide an array on c#?
  • Android NFC read Tags issue. Activity starts each time on data received
  • How to change default stop edit behavior in jtable
  • openssl handshake failed
  • Returning this from a constructor function in JS
  • How to resolve docker host names (/etc/hosts) in containers
  • What is this strange character in chrome's resource css viewer?
  • removing last 3 characters on a file (file extension)
  • Time out Error in send mail
  • android.support.v7.widget.Toolbar VectorDrawableCompat IllegalStateException when using support lib
  • Content-Length header not returned from Pylons response
  • Mysterious problem with floating point in LISP - time axis generation
  • How to know when stdin is empty if it contains EOF?
  • Play WS (2.2.1): post/put large request
  • Refering to the class itself from within a class mehod in Objective C
  • Paperclip, set path outside of rails root folder
  • How to access EntityManager inside Entity class in EJB3
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • How to add date and time under each post in guestbook in google app engine
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • vba code to select only visible cells in specific column except heading
  • Symfony2: How to get request parameter
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Proper folder structure for lots of source files
  • Load html files in TinyMce
  • coudnt use logback because of log4j
  • need help with bizarre java.net.HttpURLConnection behavior
  • Append folder name and increment by 1 using batch script
  • Reading document lines to the user (python)