CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    11

    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

  2. #2
    Join Date
    Oct 2000
    Location
    India
    Posts
    4,620

    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.
    All luck and have a great day.

    Regards,
    V.Girish

    Visit www.geocities.com/contactgirish for Source code, Tutorials, FAQs and Downloads.

  3. #3
    Join Date
    Oct 2000
    Location
    India
    Posts
    4,620

    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);
    All luck and have a great day.

    Regards,
    V.Girish

    Visit www.geocities.com/contactgirish for Source code, Tutorials, FAQs and Downloads.

  4. #4
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    11
    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

  5. #5
    Join Date
    Oct 2000
    Location
    India
    Posts
    4,620

    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.
    All luck and have a great day.

    Regards,
    V.Girish

    Visit www.geocities.com/contactgirish for Source code, Tutorials, FAQs and Downloads.

  6. #6
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    11
    Thanks very much Girish.

    I will try it out and revert back to you.

    Bikash

  7. #7
    Join Date
    May 2002
    Location
    Russia
    Posts
    1,571
    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
  •  





Click Here to Expand Forum to Full Width

Featured