0

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

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.

Community
  • 1
  • 1
Ajith K
  • 593
  • 6
  • 13
  • What is the path length of this folder? – foxidrive Oct 09 '14 at 09:07
  • I am getting this error for more than one folder and all of those folder path length is more than 260. – Ajith K Oct 09 '14 at 10:06
  • You may be able to use `pushd` but I can't test it here without knowing the details to reproduce the problem and figure a way around it. Likewise you maybe able to shorten the path for the purpose of getting the file list - without details it's hard to say. – foxidrive Oct 09 '14 at 10:22
  • i am trying to find out the size of each folder present in a location, which i given as an argument to my script. So for this i have to go through all sub folders present inside each folder and have to find out the total size of the files present inside all those folders. I am using the **dir** command for the same.So some times it happens such that, when the script going deep inside the folders to calculate the file size, the length of the folder path become more larger than 260 characters. So i am getting error messages in that cases. I hope now my problem is clear to you – Ajith K Oct 09 '14 at 11:27
  • I understood that. Without knowing the directory depth and character length and if it is on a local hard drive or a network - you know, actual details about the task, then it's hard to help you. – foxidrive Oct 09 '14 at 14:14
  • The location is on network not on my local hard drive. About the directory depth, the folder path is like network_ip\folder1\folder2\folder3\.... and it is the 16th folder and character length is 264 for the first error i am getting. – Ajith K Oct 10 '14 at 05:11
  • Since you got a good answer on superuser for the same question, you could delete this question here!? – Werner Henze Dec 04 '14 at 12:54

1 Answers1

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.

MC ND
  • 69,615
  • 8
  • 84
  • 126