2

Using C++ ( in visual studio 2013), how can I write a piece of code that searches the system path for a particular file and returns the file's full path? I only have the base name of the file. By system path, I mean the path environment variable in windows (you can see the path by typing "set path" on a command window).

For example. I like to have something like,

file_name = "my_test.xml"
path = find_file(fine_name);

Thanks in advance


I figured out the answer. Below is a piece of code that does exactly what I was looking for:

#include <iostream>
#include <Shlwapi.h>

wchar_t* get_file_full_path(const wchar_t file_name[]){
    wchar_t buf[MAX_PATH];
    wcscpy_s(buf, wcslen(file_name) + 1, file_name);
    if (PathFindOnPath(buf, NULL)) {
        std::wcout << file_name << " found." << std::endl;
        std::wcout << buf << std::endl;
    }
    else {
        std::wcout << "Could not find the file " << file_name << " on the system path.";
    }
    return buf;
}
MrAliB
  • 799
  • 9
  • 17
  • 1
    This may be helpful http://www.cplusplus.com/forum/unices/104524/ http://stackoverflow.com/questions/24447540/c-how-to-search-files-in-a-directory-with-certain-name – javaDeveloper Feb 02 '16 at 06:54
  • Are you mentioning the [PATH](https://en.wikipedia.org/wiki/PATH_%28variable%29) variable? Scanning it seems easy. Or do you want to find a file in a deep file hierarchy, knowing only its basename? – Basile Starynkevitch Feb 02 '16 at 06:57
  • If your OS windows consider https://msdn.microsoft.com/en-us/library/windows/desktop/aa364418%28v=vs.85%29.aspx – VolAnd Feb 02 '16 at 06:57
  • @MrAliB What is '*the system path*'?! – Biffen Feb 02 '16 at 07:01
  • 1
    You can just loop over the directories in the system path (PATH variable) and check each one. Or in the command interpreter you can use the `where` command. I fail to see the problem? Voted to close as unclear. – Cheers and hth. - Alf Feb 02 '16 at 07:08
  • Perhaps the [SearchPath](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365527(v=vs.85).aspx) function might help. – Jonathan Potter Feb 02 '16 at 07:24
  • Is your question aimed to find source files with cpp or do you need a tool to find your source files? if second then this open source tool can help you http://sourceforge.net/projects/sourcenav/ –  Feb 02 '16 at 08:00
  • I edited the question hoping that is clearer now. – MrAliB Feb 02 '16 at 15:22
  • @Cheersandhth.-Alf, I figured out the answer to this question but since it was put on hold, I can't post it. "where", but there is another way that I also like to share with the community. Can you or someone else take out the hold so that I can post my answer? – MrAliB Feb 02 '16 at 21:10
  • @MrAliB: Editing the question was the right thing to do. But now I would have voted to close it for another reason, namely, too broad. Because up front I can think of at least three simple ways to do this: using `SearchPath` API function, redirecting result of `where` to a file, or just iterating over the `PATH` components and checking each. More advanced ways include querying Microsoft Index Server, or using Google search. I assume the solution you're thinking of is one of those mentioned. But others may of course evaluate this differently and help out with **voting to reopen** the question. – Cheers and hth. - Alf Feb 02 '16 at 21:45
  • @Cheersandhth.-Alf My solution is neither one of the ones you mentioned. It is a shame that you won't allow me to share it. It took me an hour or so to get it to work. I benefit from Stackoverflow a lot and wanted to give back. In my humble opinion, two of your approaches are hacks, especially the iterating over the paths. I am not familiar with SearchPath. – MrAliB Feb 02 '16 at 22:32
  • @MrAliB You can ask a question more specific to your novel solution. And yes you can [answer your own question](http://stackoverflow.com/help/self-answer) to do exactly what you're talking about now, sharing. As a concrete example (you can copy the lead-in text by editing it and just cancel the edit), see e.g. my old [question about avoiding cruft for `enable_if`](http://stackoverflow.com/questions/20945362/how-can-i-avoid-writing-value-and-type-when-using-stdenable-if-cpp). By the way, I don't decide about closing or reopening, I'm just an ordinary SO user. These things are decided by voting. – Cheers and hth. - Alf Feb 02 '16 at 23:03

1 Answers1

2

You can check the system path first, and if it does not find it there, you need to do a search for the file in the machine. Depending on you dev environment, there are different ways of doing it. Assuming you are using a Windows system, this is an example:

#include <Windows.h>
#include <iostream>
int main()
{
    WIN32_FIND_DATA file;
    HANDLE search_handle=FindFirstFile(L"C:\\*",&file);
    if (search_handle)
    {
        do
        {
            std::wcout << file.cFileName << std::endl;
        }while(FindNextFile(search_handle,&file));
        CloseHandle(search_handle);
    }
}
Harriet
  • 1,633
  • 5
  • 22
  • 36
  • This is a very relevant answer. It prints out all the files and folder in the C:\ directory. What if I need to find a file that I have no idea where it is and all I know is that it is in one of the directories on the path? – MrAliB Feb 02 '16 at 07:33
  • You should then check each location on your PATH. You need to read the PATH from your environment variables, usually it is ";" delimited. Then you replace each of the locations into the place where the program currently looks into C:\\* directory. – Harriet Feb 03 '16 at 06:42
  • Please take a look at the solution I posted to at the top. I feel it is more straightforward. Thanks for your input to this question. – MrAliB Feb 03 '16 at 15:53