CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    how search\find a folder name?

    i have these function that i can find a folder name:
    Code:
    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;
    }
    /// using:
    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 function works but it's slow.. sometimes can take 2 minutes for do the job.
    can anyone advice me more for be more faster?

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: how search\find a folder name?

    What exactly is slow?
    What exactly are you trying to do?
    Through how many files/folders do you need to search?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: how search\find a folder name?

    Also, since C++17, there is a standard way to work with the filesystem, it's called std::filesystem. More details: https://en.cppreference.com/w/cpp/header/filesystem
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how search\find a folder name?

    1 program can be installed anywhere on C drive. so my function using a vector string with 2 folders names, i can search for the entire folder path... doing these search and depending what you have installed it can take time for find it

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: how search\find a folder name?

    Quote Originally Posted by Cambalinho View Post
    1 program can be installed anywhere on C drive. so my function using a vector string with 2 folders names, i can search for the entire folder path... doing these search and depending what you have installed it can take time for find it
    Did you write this program, if so modify it so it registers a key in the registry during runtime? If you didn't write this program, does it use an install program to install it? If so, it may already register a key in the registry.

    Does the app expose any COM servers? If so, it already registers its location in the registry.

    If the app is registered in the registry, all you have to do is look for the key in the registry - it's way faster doing this than searching through local harddrives.

    Btw, if you app doesn't register itself, do yourself and your users a favor and fix it so it does.

    This thing reminds me of a issue we ran into testing 3rd party applications about 20 years ago. In automated testing, one app would time out during an install. It was doing the "search the drive for previous app installs" trick. Unfortunately, if any network drives were mapped, it would search those too.

    Using this approach to determine if an app has been installed is bad business.

  6. #6
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how search\find a folder name?

    Arjay it's the GNU free compiler.. i\we can download it and install it. the rest about registry or something i don't know it
    "Using this approach to determine if an app has been installed is bad business."
    you have right, but it's for do it just once and save it on configuration file
    Last edited by Cambalinho; December 17th, 2018 at 05:23 PM.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: how search\find a folder name?

    Quote Originally Posted by Cambalinho View Post
    Arjay it's the GNU free compiler.. i\we can download it and install it. the rest about registry or something i don't know it
    "Using this approach to determine if an app has been installed is bad business."
    you have right, but it's for do it just once and save it on configuration file
    What about the next time you install the app? Are you going to have search the file system again to find the app or its configuration file? If so, you are simply doing it wrong.

  8. #8
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how search\find a folder name?

    i get your point and you have right... but i don't know how can i get the folder name.. maybe it's on registry, but i don't know how can i get it
    the Windows API have functions for read registry, but i don't know what to find and where

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: how search\find a folder name?

    Quote Originally Posted by Cambalinho View Post
    i get your point and you have right... but i don't know how can i get the folder name.. maybe it's on registry, but i don't know how can i get it
    the Windows API have functions for read registry, but i don't know what to find and where
    You start by opening the tool regedit.exe and do a search for the name of the exe you wish to find. Search the whole registry and record the key(s) that you find the contain values of the name of your exe.

    Post the keys back here.

    Btw, the registry is kind of like a system database for Windows. If you are programming on Windows, it's a good idea to understand how it works.

  10. #10
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how search\find a folder name?

    on search tool:
    i used "x86_64-w64-mingw32-g++.exe", but hace '++'...
    so try, too, "x86_64-w64-mingw32-gcc-7.2.0.exe"..
    i even tryied "mingw".. but nothing is found
    so what you can tell me more?

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: how search\find a folder name?

    Why do you want to determine if the MINGWG (GNU compiler for Windows) is already installed or not?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how search\find a folder name?

    yes
    Name:  Sem Título.jpg
Views: 609
Size:  29.9 KB

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: how search\find a folder name?

    No, I asked why you wanted to know if minggw is installed or not.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how search\find a folder name?

    i need just the entire folder name.. is what the function help me to find:
    C:\Program Files\mingw-w64\x86_64-7.2.0-win32-seh-rt_v5-rev1\mingw64\bin

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: how search\find a folder name?

    OK. On a system on which you have mingw installed, open a command prompt, type in

    set > set.txt

    and reply to this post with the file set.txt (created in the above statement) as an attachment.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 2 12 LastLast

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