Recurring search for files and files



  • We need to search files/ files in subcategories. Path This is the way to the original folder, the argument. b It's a search mask. The problem is the algorithm goes through all the files and submarines on the CD, but only the files from the reference pack are found. Path

    void FindFileRecursive(string  & Path, char *b, HWND fList)
    {
        bool flag = false;
        static int i = 0;
    
    WIN32_FIND_DATA FindData;
    string  Mask = Path + "*";
    HANDLE fFind = FindFirstFile(Mask.c_str(), &FindData);
    
    if (INVALID_HANDLE_VALUE != fFind)
    {
        do
        {
            if (Find::IsNotDotName(&FindData.cFileName[0]))
            {
                string FullPath = Path + &FindData.cFileName[0] + "\\";
                if (Find::IsDirectory(FindData.dwFileAttributes))
                    FindFileRecursive(FullPath, b, fList);
                else
                {
                    _finddata_t fd;
                    int OK = _findfirst(FullPath.c_str(), &fd);
                    int result = OK;
                    INT i = 0;
                    while (result != -1)
                    {
                        char *q = NULL;
                        if (b[0] == '*')
                        {
                            ++b;
                            flag = true;
                        }
                        if (flag)
                            q = strstr(fd.name, b);
                        if (strncmp(b, fd.name, strlen(fd.name)) == 0 || q != NULL)
                        {
                            LVITEM LvItem;
                            memset(&LvItem, 0, sizeof(LvItem));
                            LvItem.mask = LVIF_TEXT;
                            LvItem.cchTextMax = 256;
                            LvItem.iItem = i;        
                            LvItem.iSubItem = 0;    
                            LvItem.pszText = fd.name;
    
                            SendMessage(fList, LVM_INSERTITEM, 0, (LPARAM)&LvItem); 
                            char Temp[100];
                            LvItem.iSubItem = 2;
                            sprintf_s(Temp, "%s", fd.name);
                            LvItem.pszText = Temp;
                            SendMessage(fList, LVM_SETITEM, 0, (LPARAM)&LvItem); 
    
    
                            LvItem.iSubItem = 3;
                            sprintf_s(Temp, "%f", fd.size);
                            LvItem.pszText = "0";
                            if (fd.size != NULL)
                                LvItem.pszText = Temp;
                            SendMessage(fList, LVM_SETITEM, 0, (LPARAM)&LvItem);
    
                            LvItem.iSubItem = 1;
                            ctime_s(Temp, 30, &fd.time_write);
                            LvItem.pszText = Temp;
                            SendMessage(fList, LVM_SETITEM, 0, (LPARAM)&LvItem);
                            i++;
                        }
                        result = _findnext(OK, &fd);
                    }
                }
            }
        }
        while (NULL != FindNextFile(fFind, &FindData));
    }
    FindClose(fFind);
    

    }



  • From the beginning, I recommend that you use this announcement to use WinApi functions:

    typedef std::basic_string<TCHAR> StdString;
    

    You've got a mistake in filtering files, something dark comes up. Why are you doing her manually when FindFirstFile I know it's fine. That's what the function should look like:

    void ScanFolder(const StdString & folder, const StdString & mask, DWORD attrs, HWND fList)
    {
        WIN32_FIND_DATA find_data   = {0};   
        HANDLE          find_handle = FindFirstFile(  (folder + mask).c_str()
                                                    , &find_data         );
    
    if(INVALID_HANDLE_VALUE == find_handle)
    {
        // ругаемся
        return;
    }
    else
    {
        do
        {
            if(   (attrs &amp; find_data.dwFileAttributes) 
                &amp;&amp; !Find::IsNotDotName(find_data.cFileName) )
            {
                // обрабатываем найденое
            }
        }
        while(FindNextFile(find_handle, &amp;find_data))
    
        FindClose(find_handle);
    }
    

    }

    void FindFileRecursive(StdString start_path, const StdString & mask, DWORD attrs, HWND fList)
    {

    start_path = rtrim(start_path) + _T(\\);
    
    ScanFolder(start_path, mask, attrs, fList);
    
    WIN32_FIND_DATA folder_data = {0};
    HANDLE          folder_find = FindFirstFileEx
    (
          (start_path + _T("*")).c_str()
        ,  FindExInfoStandard
        , &amp;folder_data         
        ,  FindExSearchLimitToDirectories // ищем папки
        ,  NULL
        ,  FIND_FIRST_EX_LARGE_FETCH
    );
    
    if(INVALID_HANDLE_VALUE == folder_find)
    {
        // ругаемся
        return;
    }
    else
    {
        do
        {
            if(   (FILE_ATTRIBUTE_DIRECTORY &amp; folder_data.dwFileAttributes) 
                &amp;&amp; !Find::IsNotDotName(folder_data.cFileName)       )
            {
                FindFileRecursive(  start_path + folder_data.cFileName
                                  , mask
                                  , attrs
                                  , fList  );
            }
        }
        while(FindNextFile(folder_find, &amp;folder_data))
    
        FindClose(folder_find);
    }
    

    }

    Structural cut function (use) https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring 😞

     static inline StdString & rtrim(StdString & s)
    {
    s.erase
    (
    std::find_if( s.rbegin()
    , s.rend()
    , std::not1(std::ptr_fun<int, int>(std::isspace))).base()
    , s.end()
    );
    return s;
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2