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
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:
Returns a list like:
And:
Returns:
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:
An example to fit the UNC path you gave in your comment, should be specified like this: