-
December 11th, 2018, 01:25 PM
#1
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?
-
December 17th, 2018, 02:47 PM
#2
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?
-
December 17th, 2018, 02:49 PM
#3
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
-
December 17th, 2018, 03:05 PM
#4
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
-
December 17th, 2018, 05:11 PM
#5
Re: how search\find a folder name?
Originally Posted by Cambalinho
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.
-
December 17th, 2018, 05:21 PM
#6
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.
-
December 18th, 2018, 12:16 AM
#7
Re: how search\find a folder name?
Originally Posted by Cambalinho
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.
-
December 18th, 2018, 07:56 AM
#8
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
-
December 18th, 2018, 10:37 AM
#9
Re: how search\find a folder name?
Originally Posted by Cambalinho
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.
-
December 18th, 2018, 11:00 AM
#10
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?
-
December 18th, 2018, 11:08 AM
#11
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)
-
December 18th, 2018, 11:17 AM
#12
Re: how search\find a folder name?
yes
-
December 18th, 2018, 11:19 AM
#13
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)
-
December 18th, 2018, 11:21 AM
#14
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
-
December 18th, 2018, 11:36 AM
#15
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|