-9

Is there a way to globally search for a file name + .exe or whatever, and then get it's directory. That means, let's say i have a file called "food.txt" in my deep C drive. I want to get it's directory, no matter where i put it in my pc by just typing it's name + the .txt.

Isn't there a like a tag or something for "global directory". I mean like "\global\food.txt"? Thank you!

So the thing that i'm trying to do is: When I type an existing file name with the .txt, i want to open this file, no matter where in my pc it is located at. Also i won't have two duplicates with the same name, don't worry :D

Code:

void open_program(string target_name) {

    cout << "Opening..." << endl;

    string final_directory = target_name;
    ShellExecuteA(NULL, "open", final_directory.c_str(), NULL, NULL, SW_SHOWNORMAL);
    cout << final_directory;

}
joro.jur
  • 21
  • 3
  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Feb 17 '17 at 07:47
  • 1
    You would need to recursive pass through all directories and files and find the one with the specified name, then you will know it's path for sure. – bitcell Feb 17 '17 at 07:47
  • Nah, I'm not looking for any additional addons or stuff like that. I only work with build-it stuff. But thank you. – joro.jur Feb 17 '17 at 07:52
  • 1
    @bitcell and still you will not be sure of that path cause there might be x files food.txt in a system in different places and having different content? Author of question: are you sure of what you are asking may be try to explain a problem cause there is no solution to be sure of first randomly found file somewhere on C drive to be one you are looking for for sure. Could use checksums, etc; but I feel you don't need that. – Drako Feb 17 '17 at 07:53
  • @Drako, well... he said "a file", so I assume that any file with that name is the correct one. – bitcell Feb 17 '17 at 07:56
  • may be this is his answer: http://stackoverflow.com/questions/3071665/getting-a-directory-name-from-a-filename I guess. – Drako Feb 17 '17 at 07:57
  • @Drako, "Also i won't have two duplicates with the same name, don't worry :D", see? – bitcell Feb 17 '17 at 07:57
  • @Drako i don't thing this is the thing i'm looking for, because i want to get it's directory, without entering it's directory :D – joro.jur Feb 17 '17 at 08:00
  • @LightSpellBG, you are being downvoted because you didn't provide any code from your side, and you are expecting us to write the code for you. Show us what you have tried, what it didn't work and we can help you from there. – bitcell Feb 17 '17 at 08:00
  • @bitcell thank you, for the advice :D – joro.jur Feb 17 '17 at 08:02
  • @LightSpellBG, short answer: NO, it's not possible to get the path of the file without searching for it first. You could also write code that does the indexing of anyfile in your system, that way you could get the path of the file really quick, but you still need to search for them, first of all. – bitcell Feb 17 '17 at 08:02
  • Soo how can i look through all the files in my pc and check if each file matches with the name? – joro.jur Feb 17 '17 at 08:03
  • @LightSpellBG, google search that, you will find a ton of examples. – bitcell Feb 17 '17 at 08:05
  • I'm looking but i can't anything. – joro.jur Feb 17 '17 at 08:07
  • @LightSpellBG, a simple "c++ file search" should give you a lot of examples :| – bitcell Feb 17 '17 at 08:10
  • I doesn't give me the thing i look for. I just want like: while(files_left != 0) { file.getFileName... } Is it that hard to just give me an example. – joro.jur Feb 17 '17 at 08:12
  • That's it, i quit c++. I guess noone what's to help here. – joro.jur Feb 17 '17 at 08:15
  • if no one would want to help you there would be no comments and reading; just your problem is either not clearly understood or as few already told you - the answer is: "it's not possible to do so"; if you think it is possible - then try to rephrase your question, seems nobody got it right your way. – Drako Feb 17 '17 at 08:51

1 Answers1