|
-
November 29th, 2002, 01:46 AM
#1
query directory for size and number of files
Is there any API / Class that can be used to query the "Size" and the "Number of Files" in a directory
-
November 29th, 2002, 01:51 AM
#2
Hi
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.
-
November 29th, 2002, 01:55 AM
#3
One of the fastest ways
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);
-
November 29th, 2002, 02:09 AM
#4
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
-
November 29th, 2002, 02:16 AM
#5
Hi
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.
-
November 29th, 2002, 02:30 AM
#6
Thanks very much Girish.
I will try it out and revert back to you.
Bikash
-
November 29th, 2002, 05:13 AM
#7
2VGirish: Sinse you are use CString why you don't use CFindFile?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|