Solved

size of directory

Posted on 1998-12-04
6
249 Views
Last Modified: 2010-04-15
How to find out the size of a directory (i.e.) all its files and subdirectories.
Thank you.
Birendra
0
Comment
Question by:birenkd
6 Comments
 

Author Comment

by:birenkd
ID: 1254839
Its very urgent
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1254840
df .
0
 
LVL 1

Expert Comment

by:Staplehead
ID: 1254841
birenkd,

are you looking for an environment-specific answer, or a general purpose C code answer?

if you're in a unix environment, then the "df" command will do it for you...

if you're in a dos environment, you could play around with the "dir/s" command; i believe that its last line of output will tell you the total size of all the files it found.

Or, are you looking for an algorithm or code in C which will traverse a directory tree and note the sum of the sizes of all files found?

Larry
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
ID: 1254842
Assuming you mean under 95/98/NT

use FindFirstFile/FindNextFile/FindClose to iterate thru the files in a directory. This will tell you all the subdirectories as well (including . and .. subdirectories which you should ignore).  Remember FindFirstFile takes a wild card path therefore add "*.*" to the search path

For each item you get back a WIN32_FIND_DATA structure.  Which contains the size of file.  You can add this up to give total size of files.  If you meet a subdirectory, then recurse into the function to add up the total for a subdirectory, i.e.

void DirectoryTotal( const char * szDirectory, int * pTotal )
{
/* iterate thru files using using FindFirstFile etc. */
/* if a file, add to the total (*pTotal) += size */
/* if a directory and not "." or ".." then make a temporary variable with szDirectory strcat with the subdirectory name and call DirectoryTotal with sub directory name and pTotal */
}

all the funtion :-

int ntotal
DirectoryTotal( "C:\", &ntotal ) ;
printf( "total files=%d bytes", ntotal ) ;


On other operating systems there is usually a compiler/OS specific function to do directory search, in <direct.h>


0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1254843
char szCleanDir[MAX_PATH+10] ;
char szSearchPath[MAX_PATH+5] ;
WIN32_FIND_DATA fd ;
HANDLE hh ;
int ll ;
BOOL bOK = TRUE ;

/* Make sure dir has final slash */
strcpy( szCleanDir, szDirectory ) ;
ll = strlen(szCleanDir) ;
assert( ll > 0 ) ;
if ( szCleanDir[ll-1] != '\\' )
{
strcat( szCleanDir, "\\" ) ;
}

/* Generate search path */
strcpy( szSearchPath, szCleanDir ) ;
strcat( szSearchPath, "*.*" ) ;

/* Do search */
hh = FindFirstFile( szSearchPath, &fd ) ;
while ( ( hh != NULL ) && ( bOK ) )
{
if ( ( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) != 0 )
{
/* it's a directory */
char szSubDirectory[MAX_PATH+10] ;
if ( strcmp( (char *)fd.cFileName, "." ) == 0 ) continue ;
if ( strcmp( (char *)fd.cFileName, ".." ) == 0 ) continue ;
strcpy( szSubDirectory, szCleanDir ) ;
strcat( szSubDirectory, (char *)fd.cFileName ) ;
DirectoryTotal( szSubDirectory, pTotal ) ;
} else
{
/* it's a file */
(*pTotal) += fd.nFileSizeLow ;
}

bOK = FindNextFile( hh, &fd ) ;

} /* while* /

if ( hh != NULL ) FindClose(hh) ;

}



0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1254844
The above code goes inside the DirectoryTotal function, basically implementing the Win32 Algorithm.

Couple of points
1. File Sizes can exceed that of an int, (64 bit number).  Therefore if you want to take this into account change type of pTotal and modify "it's a file" section

2. Sorry if any typos, but hopefully you get the point
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with form project 66 317
Test against App 49 133
SCANF - LIMIT THE NUMBER OF CHARARACTERS 1 60
List out all word 7 261
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now