Windows – The Directory name is too long in batch

batch filewindows

I already asked the question in stack overflow, but did not get any answer from anyone. So i am asking the same question here also.

I am trying make a program which will calculate size of all folder present in the specified location or the current location using a batch script and i am writing it into a .csv file

I followed this question in stackoverflow

https://stackoverflow.com/questions/21711180/how-to-list-all-folder-with-size-via-batch-file

and my code is

`@echo off
 setlocal disabledelayedexpansion
 set "folder=%~1"
 Set "Value=0"
 if not defined folder set "folder=%cd%"
  (for /d %%a in ("%folder%\*") do (
    set "size=0"
    for /f "tokens=3,5" %%b in ('dir /-c /a /w /s /x "%%~fa\*" ^| findstr /b /c:"  "')do if "%%~c"=="" set "size=%%~b"
   setlocal enabledelayedexpansion
   echo(%%~nxa , !size!
   endlocal
  ))>>foldersize.csv 
endlocal
exit /b`

When i run the code, i am getting an error

The directory name "dir_name" is too long

I already knew that the maximum length for a windows path name is 260 character and that might be the reason for this error.

Is there any way I can solve this problem ??

Thanks in advance for every response.

Best Answer

On an NTFS partition, the maximum path-length is about 32,760 characters (something slightly less than 32,767). But, MAX-PATH is still 260. Paths longer than "MAX-PATH" are still accessible and are referred to as an "extended-length path".

According to this page: Maximum Path Length Limitation

To specify an extended-length path, use the "\\?\" prefix. For example, "\\?\D:\very long path".

Also keep in mind that an "extended-length path" is always an "absolute" path. You cannot specify a "relative form" of an "extended-length path".

I haven't tried running your exact script, but I've done a little testing from a command prompt and it appears it's workable.

For example:

@for /f "usebackq delims=" %f in (`dir /s /b "\\?\c:\Blah"`) do @echo %f

Returns a list like:

\\?\c:\Blah\test
\\?\c:\Blah\test\aaa
\\?\c:\Blah\test\bbb
\\?\c:\Blah\test\aaa\folder1
\\?\c:\Blah\test\aaa\folder2
\\?\c:\Blah\test\aaa\folder1\File1.txt
\\?\c:\Blah\test\aaa\folder1\File2.txt
\\?\c:\Blah\test\aaa\folder2\File1.txt
\\?\c:\Blah\test\aaa\folder2\File2.txt
\\?\c:\Blah\test\bbb\folder1
\\?\c:\Blah\test\bbb\folder2
\\?\c:\Blah\test\bbb\folder1\Diff.txt
\\?\c:\Blah\test\bbb\folder1\Same.txt
\\?\c:\Blah\test\bbb\folder2\Diff.txt
\\?\c:\Blah\test\bbb\folder2\Same.txt

And:

C:>dir /-c /a /w /s /x "\\?\c:\Blah" | findstr /b /c:"  "

Returns:

           0 File(s)              0 bytes
           0 File(s)              0 bytes
           2 File(s)            660 bytes
           2 File(s)            654 bytes
           0 File(s)              0 bytes
           2 File(s)            654 bytes
           2 File(s)            660 bytes
 Total Files Listed:
           8 File(s)           2628 bytes
          20 Dir(s)      3268722688 bytes free

Which doesn't look like it's exactly what you need, so your "dir/findstr" command will need some adjustment.

I don't have any path\files that are over 260 characters, so I can't tell what happens with "very long" paths, but it should work.


If the Path you are using is a UNC path the page linked above says this:

The "\\?\" prefix can also be used with paths constructed according to the universal naming convention (UNC). To specify such a path using UNC, use the "\\?\UNC\" prefix. For example,
"\\?\UNC\server\share", where "server" is the name of the computer and "share" is the name of the shared folder. These prefixes are not used as part of the path itself.

An example to fit the UNC path you gave in your comment, should be specified like this:

dir /-c /a /w /s /x "\\?\UNC\10.0.0.10\folder1\folder2" | findstr /b /c:" "
Related Question