2

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.

@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION

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" 

ENDLOCAL
2
  • Does it ave to be batch? PowerShell is the modern equivalent and does all sorts of great thing efficiently. Nov 18, 2013 at 15:12
  • There are indeed PowerShell scripts to determine path length: stackoverflow.com/questions/12697259/… 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. Nov 18, 2013 at 15:40

3 Answers 3

0

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!
)
2

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.

2
  • 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, 2013 at 16:45
  • Your sample expression is also true for a with 13 characters, ie not shorter than 13. Am I missing something? Nov 19, 2013 at 18:42
1

You could filter the DIR output by a program using regex.
findstr could do that, but unfortunetly the regex is very limited and it supports only search patterns up to 254 characters.
The code could be:

@echo off
setlocal enabledelayedexpansion
set "_LIMIT_=%~1"

set _PATTERN_=
for /l %%n in (1,1,%_LIMIT_%) do (set "_PATTERN_=!_PATTERN_!.")

dir /o:-n /b /s | findstr /r "/c:%_PATTERN_%"

If you don't mind using third party programs you could use grep instead. This is very easy:

@echo off
setlocal
set "_LIMIT_=%~1"

dir /o:-n /b /s | grep -E .{%_LIMIT_%}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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