2

I'm looking for a script that can be run from command line (batch \ PowerShell) that will go over a folder and its sub folders and will return a number which is a length of the longest file path.

I already saw some batch and PowerShell scripts like
How do I find files with a path length greater than 260 characters in Windows?
but none of them works satisfy my request.

Note that it's possible that file path will be more than 256 characters

Community
  • 1
  • 1
Epligam
  • 741
  • 2
  • 14
  • 36

1 Answers1

6

PowerShell:

((Get-ChildItem -Recurse).FullName  | Measure-Object -Property Length -Maximum).Maximum

Command line:

powershell -exec Bypass -c "((dir -rec).FullName | measure Length -max).Maximum"

Edit

related to error: Get-ChildItem : The specified path, file name, or both are too long: read Maximum Path Length Limitation and related [PowerShell]-tagged StackOverflow threads.

PS D:\PShell> ((Get-ChildItem "D:\odds and ends" -Directory -Recurse).FullName  | Measure-Object -Property Length -Maximum).Maximum
242

PS D:\PShell> ((Get-ChildItem "D:\odds and ends" -Recurse -ErrorAction SilentlyContinue).FullName  | Measure-Object -Property Length -Maximum).Maximum
242

Note that -ErrorAction SilentlyContinue in above command merely suppresses displaying of error messages. However, I know that the latter 242 value returned is wrong.

My workaroud applies cmd /C dir /B /S instead of (Get-ChildItem -Recurse).FullName as follows:

PS D:\PShell> $x = (. cmd /C dir /B /S "D:\odds and ends")
PS D:\PShell> $y = ( $x | Measure-Object -Property Length -Maximum).Maximum
PS D:\PShell> $y
273
PS D:\PShell> $z = $x | Where-Object { $_.Length -gt 260 }
PS D:\PShell> $z.GetTypeCode()
String
PS D:\PShell> $z
D:\odds and ends\ZalohaGogen\WDElements\zalohaeva\zaloha_honza\Music\Jazz\!Kompilace\Saint Germain des Pres Cafe Vol. 1 to 8 - The Finest Electro Jazz Complication\Saint Germain Des Pres Cafe Vol. 7 - The Finest Electro Jazz Complication\CD 1\Configuring and Using Inte.txt
PS D:\PShell>
Community
  • 1
  • 1
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • I got this error Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters. but it is working after running this: From http://www.powershellmagazine.com/2012/07/24/jaap-brassers-favorite-powershell-tips-and-tricks/: Get-ChildItem –Force –Recurse –ErrorAction SilentlyContinue –ErrorVariable AccessDenied – Epligam Jan 19 '17 at 15:24
  • @Epligam answer updated for **really long paths**. Solution from PowerShellMagazine does not change the latter wrong value of `242` for me. – JosefZ Jan 19 '17 at 15:45