How-to: List files that exceed the 256 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 maximum Windows API MAX_Path is 260 characters, subtract 3 characters for the drive and colon: and 1 character for a terminating NULL and you have a maximum 256 characters that can be used for the pathname.

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

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

The script below will list any files that exceed this limit.

@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:~255,1!"=="" echo Extra long name: "%%a"


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-2023
Some rights reserved