Link to home
Start Free TrialLog in
Avatar of arnold100
arnold100

asked on

Is it possible to find the path to a file on any computer, any drive, any directory and any OS?

Hi All,
I am working in VC++ 6 mfc. I need a way to find a file on any drive, any directory and any OS. I need a function that works like GetWindowsDirectory(); but for all directories.

Thank you very much for your time,
AJ.

Avatar of nonubik
nonubik

That's pretty much impossible. Imagine every OS has its different function to call for finding a file. Then GetWindowsDirectory() only looks upon an environment variable already set, it does not search on the drives for a folder that is the windows directory. And if you need to find a file on any drive, that's hard, too, if by 'file' you mean only the file name, not the full path name. Imagine you have 2 different files with the same name in 2 different folders, C:\foo.txt and c:\windows\foo.txt.
Avatar of AndyAinscow
On windows you can use CFileFind (and recursion to search directories).  I don't know what the functions would be for linux.... (BUT would your windows MFC app run on linux.... ?)
Avatar of arnold100

ASKER

No linux. I should have said all windows OS's. Sorry. There is a GetFilePath(); But It doesn't take any parameters. I don't understand how to use it. could anyone show me some code to use this function? The file I need to find is Msimn.exe. If this file was always in C:\Program Files\Outlook Express on all windows OS's It wouldn't be a problem, but I don't know that it is located here on all windows OS's.

CFile f;
f.GetFilePath(); //return the full path to a file

Now I am lost as to how to pass a file name to this function.

Thanks,
Wrong function - you need to use CFileFind as I said earlier.

This is from the help files.
This small program recurses every directory on the C:\ drive and prints the name of the directory.

#include <afx.h>
#include <iostream>

using namespace std;

void Recurse(LPCTSTR pstr)
{
   CFileFind finder;

   // build a string with wildcards
   CString strWildcard(pstr);
   strWildcard += _T("\\*.*");

   // start working for files
   BOOL bWorking = finder.FindFile(strWildcard);

   while (bWorking)
   {
      bWorking = finder.FindNextFile();

      // skip . and .. files; otherwise, we'd
      // recur infinitely!

      if (finder.IsDots())
         continue;

      // if it's a directory, recursively search it

      if (finder.IsDirectory())
      {
         CString str = finder.GetFilePath();
         cout << (LPCTSTR) str << endl;
         Recurse(str);
      }
   }

   finder.Close();
}

void main()
{
   if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0)
      cout << "panic!" << endl;
   else
      Recurse(_T("C:"));
}


You would need to modify it so that each DIRECTORY it finds you then search that directory for the file you want.  (Another CFileFind).  I would suggest returning a bool from the Recurse function so you 'know' when you have found the file and at that point stop searching further.
Well here is what I tried:

CString Recurse(LPCTSTR pstr)
{
   CFileFind finder;
   CString strWildCard(pstr);
   strWildCard+=_T("\\*.*");
   BOOL bWorking=finder.FindFile(strWildCard);
   while(bWorking)
   {
        bWorking=finder.FindNextFile();
        if(finder.IsDots())
        {
            CString str=finder.GetFilePath();
            MessageBox(NULL,(LPCTSTR)str,"",MB_OK);
        }Recurse(str);
    }finder.Close();
    return (LPCTSTR)str;
}


Then I call sPath= Recurse("Msimn.exe"); in the program with a MessageBox(sPath); Recurse returns an empty string.

The messagebox function in Recurse never gets executed.
bWorking is always false.

Can you see what I am doing wrong?

       if(finder.IsDots())
        {
            CString str=finder.GetFilePath();
            MessageBox(NULL,(LPCTSTR)str,"",MB_OK);
        }Recurse(str);

that isn't correct

try
        if(finder.IsDirectory() && !finder.IsDots())
        {
            CString str=finder.GetFilePath();
            MessageBox(NULL,(LPCTSTR)str,"",MB_OK);
//Found a directory that is not . or .. SO recurse into it
Recurse(str);
        }



ALSO when you enter the directory (start of Recurse function) you need to search for the file you want/test for it's presence
No luck. Still get an empty string.
Please post your new code so we are clear what we are talking about.
CString Recurse(LPCTSTR pstr)
{
   CFileFind finder;
   CString strWildCard(pstr);
   strWildCard+=_T("\\*.*");
   BOOL bWorking=finder.FindFile(strWildCard);
   while(bWorking)
   {
        bWorking=finder.FindNextFile();
        if(finder.IsDirectory() &&!finder.IsDots())
        {
            CString str=finder.GetFilePath();
           //this messagebox never executes
            MessageBox(NULL,(LPCTSTR)str,"",MB_OK);
        }Recurse(str);
    }finder.Close();
    return (LPCTSTR)str;
}

Call the function in the program as:
 sPath=Recurse("Msimn.exe");

MessageBox(sPath);//is empty
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Still no luck. I still get an empty string returned. Do you know where the Msimn.exe file is stored in the NT windows OS?
Ok got it working but it doesn't find the Msimn.exe file. It checks all the directories ok.
AndyAinscow, Got it! Now I know how to find any file,in any directory and on any drive. I thank you very much for your help.

Sincerely yours,
AJ.