Folder Size

Aravinda VK via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 21 01:57:52 PDT 2017


On Saturday, 19 August 2017 at 14:19:39 UTC, Vino.B wrote:
> Hi All,
>
>   I have written a small program to find the size of folder's , 
> but the output is not as expected, hence request your help and 
> advice on any techniques to reduce the run time of the program.
>
> Requirement:
> The script has to scan several file system
>
> ("C:\\Temp\\TEAM","C:\\Temp\\PROD_TEAM", "C:\\Temp\\BACKUP")
>
> Display only the sub folder level 1 name whose name should not 
> contain *DND* and size
>
> dirEntries(i, SpanMode.shallow).filter!(a => a.isDir && 
> !globMatch(a.baseName, "*DND*"))
>
> Eg: C:\Temp\TEAM\USER_BACKUP , C:\Temp\PROD_TEAM\PROD_BACKUP , 
> C:\Temp\BACKUP\SUPPORT_BACKUP
>
> Need the folder name and size of each folder under
>  C:\Temp\TEAM\USER_BACKUP , C:\Temp\PROD_TEAM\PROD_BACKUP , 
> C:\Temp\BACKUP\SUPPORT_BACKUP
> --------
> Program
> --------
> import std.file: dirEntries, isFile, SpanMode;
> import std.stdio: writeln;
> import std.algorithm: filter;
> import std.array: array;
> import std.path;
>
> auto AgedDirlst = [ "C:\\Temp\\TEAM\\USER_BACKUP" , 
> "C:\\Temp\\PROD_TEAM\\PROD_BACKUP" , 
> "C:\\Temp\\BACKUP\\SUPPORT_BACKUP" ];
>
> void main ()
> {
> foreach (string i; AgedDirlst[0 .. $])
>      {
> 	  auto dFiles = dirEntries(i, SpanMode.shallow).filter!(a => 
> a.isDir && !globMatch(a.baseName, "*DND*")).array;
> 	  foreach (d; dFiles)
> 	        {
> 			  auto SdFiles = dirEntries(d, SpanMode.depth).array;
> 			  foreach (f; SdFiles)
> 			  writeln(f.dirName, "\t", f.size);
> 	 	      		
> 	        }
> 	 }
> writeln("*************************************************************************************");
> }
> --------
> Output:
> --------
> C:\Temp\TEAM\USER_BACKUP\DIR1       		41129
> C:\Temp\TEAM\USER_BACKUP\DIR1\dir3  		68
> C:\Temp\TEAM\USER_BACKUP\DIR1       		0
>
> C:\Temp\TEAM\USER_BACKUP\DIR2\dir4  		3410
> C:\Temp\TEAM\USER_BACKUP\DIR2\dir4  		2277663
> C:\Temp\TEAM\USER_BACKUP\DIR2       		0
> C:\Temp\TEAM\USER_BACKUP\DIR2       		1156
>
> C:\Temp\PROD_TEAM\PROD_BACKUP\dir1  		41129
> C:\Temp\PROD_TEAM\PROD_BACKUP\dir1\dir3     	68
> C:\Temp\PROD_TEAM\PROD_BACKUP\dir1  		0
> C:\Temp\PROD_TEAM\PROD_BACKUP\dir1\DND1     	36590125
> C:\Temp\PROD_TEAM\PROD_BACKUP\dir1  		0
>
> C:\Temp\PROD_TEAM\PROD_BACKUP\dir2\dir4     	3410
> C:\Temp\PROD_TEAM\PROD_BACKUP\dir2  		0
> C:\Temp\PROD_TEAM\PROD_BACKUP\dir2  		1156
>
> C:\Temp\BACKUP\SUPPORT_BACKUP\dir1\DND1         36590125
> C:\Temp\BACKUP\SUPPORT_BACKUP\dir1     		0
> C:\Temp\BACKUP\SUPPORT_BACKUP\dir2\DND2         1156
> -------------------
> Required Output
> -------------------
> C:\Temp\TEAM\USER_BACKUP\DIR1                   41197  
> (41129+68)
> C:\Temp\TEAM\USER_BACKUP\DIR2                   2282229 
> (3410+2277663+1156)
> C:\Temp\PROD_TEAM\PROD_BACKUP\dir1              36631322 
> (41129+68+36590125)
> C:\Temp\PROD_TEAM\PROD_BACKUP\dir2              4566 (3410+1156)
> C:\Temp\BACKUP\SUPPORT_BACKUP\dir1              36590125
> C:\Temp\BACKUP\SUPPORT_BACKUP\dir2              1156
>
> From,
> Vino.B

Keep a variable to add the sizes of subdirs

auto dFiles = dirEntries(i, SpanMode.shallow).filter!(a => 
a.isDir && !globMatch(a.baseName, "*DND*")).array;
ulong subdirTotal = 0;
foreach (d; dFiles)
{
         subdirTotal = 0;
         auto SdFiles = dirEntries(d, SpanMode.depth).array;
         foreach(f; SdFiles) {
                 subdirTotal += f.size;
         }
         writeln(d, "\t", subdirTotal);
}

--
Aravinda
http://aravindavk.in



More information about the Digitalmars-d-learn mailing list