birenkd
asked on
size of directory
How to find out the size of a directory (i.e.) all its files and subdirectories.
Thank you.
Birendra
Thank you.
Birendra
df .
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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) ;
}
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) ;
}
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
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
ASKER