Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to know if anyone has devised an efficient way in NT batch of computing path lengths in a given folder. This is necessary to identify files and folders which won't successfully archive to optical media, which continue to enforce 260-character path limits though NTFS now supports path lengths of up to 32,767 characters.

I have been using a batch script which works by echoing each full path to a file, measuring the size of the file, and subtracting 2 to get the character count in the path. This works well for low file counts, but takes a long time to finish for high file counts. Ideally, I would like something that works almost as fast as the 'dir' command itself.



set Limit=%~1
echo Paths being found which exceed !Limit!
echo ======================================
type NUL > "!temp!\tabulator.txt"
FOR /F "tokens=1 delims=" %%A IN ('dir /o:-n /b /s') DO (
set Test=%%A
call set Test=%%Test:~%Limit%%%.
IF !Test! NEQ . (
type NUL > "!temp!\pathlengthdeterminationtemp.txt"
echo %%A > "!temp!\pathlengthdeterminationtemp.txt"
FOR /F "tokens=1 delims=" %%H IN ('dir /s /o /b "!temp!\pathlengthdeterminationtemp.txt"') DO set StrLen=%%~zH
del "!temp!\pathlengthdeterminationtemp.txt"
set /a StrLen=!StrLen!-2
echo !StrLen!,%%A>> "!temp!\tabulator.txt"

sort "!temp!\tabulator.txt" /O "!temp!\tabulator1.txt"
del "!temp!\tabulator.txt"
TYPE "!temp!\tabulator1.txt"
del "!temp!\tabulator1.txt" 

share|improve this question
Does it ave to be batch? PowerShell is the modern equivalent and does all sorts of great thing efficiently. – AthomSfere Nov 18 '13 at 15:12
There are indeed PowerShell scripts to determine path length:… However, I'm looking for a solution that doesn't require launching the PowerShell console or changing to the folder I am interested in. The 'pathlength' script I quoted above is in my Windows path, so I can run it simply by opening a command window in the folder I am interested in. – J N Winkler Nov 18 '13 at 15:40

To check the length of an environment variable, you can do something similar to this:

set a=1234567890123

if [%a:~0,-12%]==[] (
    echo a is shorter than 13
) else (
    echo %a% is longer than 12

Expression %a:~0,-12% returns all but the last 12 characters of variable a.

Enter set /? to get details.

However, I am not sure if this also work under NT. I have tested it as CMD script under Windows 7.

share|improve this answer
Right idea, but you have got your logic mixed up. You want "%a:~13%"=="" to test if value is shorter than 13 chars. You also want quotes to protect against poison chars. Like you, I do not know if NT supports substring operation. – dbenham Nov 18 '13 at 16:45
Your sample expression is also true for a with 13 characters, ie not shorter than 13. Am I missing something? – Axel Kemper Nov 19 '13 at 18:42
Sorry, my mistake. I must have had my head up my ass when I wrote that incorrect comment. +1 for a 100% correct answer. – dbenham Nov 19 '13 at 19:42

There is no built-in command for determining the length of a string in a batch file. It necessarily involves ugly hacks like writing to a file. However, it is relatively easy to check whether a string exceeds a fixed length such as the 260 character limit. The following script grabs a 260 character substring and tests equality. If both variables do not match, the path is assumed to be too long and printed.

@echo off
setlocal enabledelayedexpansion
for /F "tokens=1 delims=" %%i in ('dir /o:-n /b /s') do (
    set filename=%%i
    set part=!filename:~0,260!
    if !filename! NEQ !part! echo !filename!
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.