0

I have written a code to iterate over the drive to find the file of specific extension and if the file is fount it'll be added to the list box but the code fails its iteration when it finds a directory. The iteration stops with the files not the directories. I want my program to search the file with a given extension even in the directories and sub-directories what shall I do?

here is my code

The variable buffer seen in my code is nothing but the drive string e.g H:\ count = 0;

int Class::countOfDocuments(wchar_t buffer[10])
{
    wchar_t driveString[MAX_PATH + 1] = { 0 };
    wcsncat_s(driveString,260,buffer,260);
    wcsncat_s(driveString, 260, L"*doc", 260);
    WIN32_FIND_DATA documents;
    HANDLE hFind; bool var = true;
    hFind = FindFirstFile(driveString, &documents);
    if (INVALID_HANDLE_VALUE == hFind)
    {
        wchar_t* no = L"No documents found";
        SendMessage(list1, LB_ADDSTRING, NULL, (LPARAM)no);
        for (int i = 0; i <= 10; i++)
        {
            SendMessage(cprogress, PBM_SETRANGE, 0, i);
            SendMessage(cprogress, PBM_SETSTEP, (WPARAM)1, 0);
            SendMessage(cprogress, PBM_STEPIT, 0, 1);
        }
        return count = -1;
    }
    else
    {
        wchar_t* Yes = L"Document found";
        perform = true;
        SendMessage(list1, LB_ADDSTRING, NULL, (LPARAM)Yes);
        while (var = FindNextFile(hFind, &documents) == TRUE)
        {
            count++;
        }

        for (int i = 0; i <= count; i++)
        {
            SendMessage(cprogress, PBM_SETRANGE, 0, i);
            SendMessage(cprogress, PBM_SETSTEP, (WPARAM)1, 0);
            SendMessage(cprogress, PBM_STEPIT, 0, 1);
            wchar_t* doc = L"Document found";
            SendMessage(list1, LB_ADDSTRING, NULL, (LPARAM)doc);
        }
        wchar_t* z = L"search complete";
        SendMessage(list1, LB_ADDSTRING, NULL, (LPARAM)z);
}
    return count;
}
user105127
  • 25
  • 1
  • 4
  • Read answer 2 for the windows solution: http://stackoverflow.com/questions/67273/how-do-you-iterate-through-every-file-directory-recursively-in-standard-c – user4581301 Mar 27 '16 at 16:16
  • Recursion. The find functions only operate in the present directory (unless they say otherwise). *For Each Directory*, you will need to change into that directory and search it. Recurse through the subdirectories until your file is found or there are no more directories. You may want to consider *traversing* the filesystem as if you were traversing a tree. – Thomas Matthews Mar 27 '16 at 17:34

0 Answers0