How-to: List files that exceed the Max_Path character limit

It is possible to create very long filenames/pathnames on NTFS that exceed the 260 character Windows API limit. This typically happens when a folder which is part of a deep hierarchy gets renamed. Very long filenames will often create errors in applications that attempt to open them (even Windows Explorer.)

The Windows API MAX_Path is 260 characters, subtract 3 characters for the drive, colon and backslash and 1 character for a terminating NULL and you have a maximum 256 characters that can be used for a filename/pathname or 259 chars including the drive letter prefix.

All .Net applications enforce the Windows API pathname limit including Windows Explorer and PowerShell.

To access very long paths use Robocopy, DIR, SUBST or the \\?\UNCPATH\ syntax.

List all the filenames with Robocopy in PowerShell:

PS C:\> $rootfolder = "E:\groups\"
PS C:\> $options = @("/l","/e","/njh","/njs","/bytes","/fp","/nc","/ndl","/njs","/r:0","/w:0","/xj")
PS C:\> robocopy $rootfolder "e:\null" $options > "E:\logs\listfiles.txt"

List only the files that exceed 256 chars with DIR, save this as xlong.cmd:

@Echo off
Setlocal EnableDelayedExpansion
:: Report all file / folder paths that exceed the 256 character limit
If {%1}=={} Echo Syntax: XLong DriveLetter&goto :EOF
Set wrk=%1
Set wrk=%wrk:"=%

For /F "Tokens=*" %%a in ('dir %wrk% /b /s /a') do (
 set name=%%a
 if not "!name:~256,1!"=="" echo Extra long name: "%%a"

Or in PowerShell, this one liner:
cmd /c dir /s /b | where-object{$_.length -gt 256}


Check the whole of the D: drive:

c:\> xlong D:\

Check the current directory on the C: drive:

c:\> xlong C:

Check the S:\workgroups\ folder:

c:\> xlong S:\workgroups\

“This report, by its very length, defends itself against the risk of being read” ~ Winston Churchill

Related commands

How-to: Long Filenames - NTFS long filenames (>260 chars)
How-to: Which.cmd - Display full path to any command.
Bash equivalent: find . | awk '{print length($0) " " $0}' | sort -n | tail -n 15

Copyright © 1999-2024
Some rights reserved