Is there any API / Class that can be used to query the "Size" and the "Number of Files" in a directory:confused:
Printable View
Is there any API / Class that can be used to query the "Size" and the "Number of Files" in a directory:confused:
To get the number of files in a specified directory, you can use
FindFirstFile and FindNextFile functions.
Take a look at them in the MSDN.
To get the size of a particular file, you can use the
GetFileSize
function. U can look it up in the MSDN too.
Or if u want a simple class to use, give me a shout and i'll do it
for you.
Hi,
This is one of the fastest ways of enumerating number of files
and the total size of all the files in a particular directory.
WIN32_FIND_DATA fd;
HANDLE hnd = ::FindFirstFile("C:\\Test\\*.*", &fd);
__int64 i64 = 0;
int nFileCount = 0;
while(hnd != INVALID_HANDLE_VALUE)
{
__int64 hi = fd.nFileSizeHigh;
hi <<= 32;
i64 += hi + fd.nFileSizeLow;
++nFileCount;
if(!::FindNextFile(hnd, &fd))
break;
}
char pIToA[20];
itoa(nFileCount,pIToA, 10);
CString strMsg("The file count is ");
strMsg += pIToA;
strMsg += ".\n\nThe total byte count is ";
_i64toa(i64, pIToA, 10);
strMsg += pIToA;
AfxMessageBox(strMsg);
Thanks very much Harish,
Using FindFirstFile and FindNextFile we will need to do the following:
loop through all the files and get the count of files and also sum up their sizes to get the total size of the directory.
1. will the size of the directory be exactly equal to the sum of the sizes of the files within it.
2. Is there any other function that I can use to directly get the "size" and "Number of Files" (in order to avoid looping through all files)
Thanks once again,
bikash
The code above is real fast. Please try it out and let me know abt
it.
For example, when this code is run in a local directory containing 2,000 files, the time taken is about 20 milliseconds. For a
network read operation, it must be somewhere around 5 to 7
times more. If u'd like to benchmark it, u can use the
SYSTEMTIME structure to verify the speed.
Thanks very much Girish.
I will try it out and revert back to you.
Bikash
2VGirish: Sinse you are use CString why you don't use CFindFile?