4

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.

  • Can you do it by using the old style 8.3 naming conventions as if you were writing code to run on Windows 3.1? – fixer1234 Oct 12 '14 at 18:02
8

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:" "
  • Thank You for your reply. In my case the location is a network address, so if I put those location into the command u gave, it will become like dir /-c /a /w /s /x "\\?\\\10.91.49.10\folder1\folder2" | findstr /b /c:" " When i run this command, I am getting an error like The filename, directory name, or volume label syntax is incorrect – ajith Oct 13 '14 at 5:17
  • @ajith - I added more to my answer to explain this... To specify a UNC path you need to use the prefix: "\\?\UNC\" like this: dir /-c /a /w /s /x "\\?\UNC\10.0.0.10\folder1\folder2" | findstr /b /c:" " – Kevin Fegan Oct 13 '14 at 7:52
  • 1
    Even after I change the command as you said ( added \\?\UNC\ ) , still I am getting the same error The filename, directory name, or volume label syntax is incorrect – ajith Oct 13 '14 at 11:06
  • 2
    I know this is an old question, but as ajith said - neither of those work for me, I prefixed it with \\?\ and \\?\UNC\ and mapped it to K: drive and did \\?\K:\ all of those still result in filename or extension is too long – StanM Jun 3 '15 at 14:55
0

The only way i see for it to work, assuming that the physical server that hold the information does not have any problem accessing the same folders, is to map a drive letter to the folder where the process of directory traversal starts, removing the computer address and share name from the path length.

protected by Community Apr 21 '16 at 11:46

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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