heres my actual code for find\search a folder:

string FindDirectory(std::string FindDirectoryName[2], std::string StartDirectory="C:")
{
    WIN32_FIND_DATA file={0};
    string str=StartDirectory+ "\\*";
    HANDLE search_handle = FindFirstFile(str.c_str(), &file);
    static bool blnFindDirectoryName=false;
    static string strFolderName ="";

    //testing if the folder is valid:
    if (search_handle != INVALID_HANDLE_VALUE)
    {
        do
        {
            //if the folder was founded, then break the loop:
            if(strFolderName!="")
                break;

            //for avoid files names and the folders "." and "..":
            if ((file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp(file.cFileName, "..") && strcmp(file.cFileName, "."))
            {
                //cout <<StartDirectory + "\\" + file.cFileName << endl;

                //testing if the folder was found
                //and add it on static strFolderName
                if(string(file.cFileName)==FindDirectoryName[0] && blnFindDirectoryName==false)
                {
                    if(FindDirectoryName[1]=="")
                    {
                        strFolderName = StartDirectory+ "\\" + file.cFileName;
                        break;
                    }
                    else
                        blnFindDirectoryName=true;
                }
                if(string(file.cFileName)==FindDirectoryName[1] && blnFindDirectoryName==true)
                {
                    //MessageBox(NULL, string(StartDirectory+ "\\" + file.cFileName).c_str(), "found it", MB_OK);
                    strFolderName = StartDirectory+ "\\" + file.cFileName;
                    break;
                }
                else
                {
                    //or continue searching:
                    FindDirectory(FindDirectoryName,StartDirectory + "\\" + file.cFileName);
                }
            }
        } while (FindNextFile(search_handle, &file) && search_handle!=NULL);
        //destroy the search_handle:
        FindClose(search_handle);
    }
    //return the search folder full path:
    return strFolderName;
}

//use it:
string File[]={"x86_64-7.2.0-win32-seh-rt_v5-rev1","mingw64"};
    string FileName1=FindDirectory(File);
    MessageBox(NULL,FileName1.c_str(),"hey", MB_OK);

these code works, but sometimes can get 2 or more minutes... is there another faster way?

Recommended Answers

All 4 Replies

How do you execute this in Windows?

Even at 2 minutes you might be doing better than Windows File Explorer (WFE.) Keep in mind that I won't go over a lot of prior discussions but will note that unless you build up some sort of cache to avoid a directory call the speed is limited by the speed of the OS, API and disk speeds.

It appears you search for files from the root so you may be traversing OS system areas which you would want to exclude. Maybe just search from the user home folder is best as your app usually doesn't need to go outside their home folder.

If you aren't looking for a home-grown file finder, or if you don't need it integrated with other software, I suggest you have a look at the free Everything Indexer from voidtools. I gives you real-tme (vurtually) instant feedback on finding files and folders by name.

i'm using Windows 7.
that program can be installed every, depends on user. that's why i need search it's folder
but the time, too, depends if the CPU is busy.
thanks for all to all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.