?
Solved

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

Posted on 2005-04-07
13
Medium Priority
?
270 Views
Last Modified: 2013-11-20
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.

0
Comment
Question by:arnold100
  • 7
  • 5
13 Comments
 
LVL 16

Expert Comment

by:nonubik
ID: 13733623
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.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 13733686
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.... ?)
0
 

Author Comment

by:arnold100
ID: 13734901
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,
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 13735071
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.
0
 

Author Comment

by:arnold100
ID: 13736177
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?

0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 13736473
       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
0
 

Author Comment

by:arnold100
ID: 13736967
No luck. Still get an empty string.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 13737021
Please post your new code so we are clear what we are talking about.
0
 

Author Comment

by:arnold100
ID: 13737264
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
0
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 200 total points
ID: 13737423
You have misunderstood.
Recurse is initially called with C: or D: - the root directory you want to start the search.
Then in the recurse function itself you need to check if the FILE you want is present.  Either using CFileFind or another way such as the CFile::GetStatus static function

This is roughly what you require

LPCTSTR Recurse(LPCTSTR pstr)
{

   CFileFind finder;
   CString strFile(pstr);
   strFile+=_T("\\msimn.exe");
   if(finder.FindFile(strFile))
{
     return pstr;   //path to where the file is
}
finder.Close();

   CString strWildCard(pstr);
   strWildCard+=_T("\\*.*");
 CString str;
   BOOL bWorking=finder.FindFile(strWildCard);
   while(bWorking)
   {
        bWorking=finder.FindNextFile();
        if(finder.IsDirectory() &&!finder.IsDots())
        {
           str=finder.GetFilePath();
           //this messagebox never executes
            MessageBox(NULL,(LPCTSTR)str,"",MB_OK);
str = Recurse(str);
if(str.GetLength())
  return str;
        }
    }finder.Close();
    return "";
}
0
 

Author Comment

by:arnold100
ID: 13737947
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?
0
 

Author Comment

by:arnold100
ID: 13738282
Ok got it working but it doesn't find the Msimn.exe file. It checks all the directories ok.
0
 

Author Comment

by:arnold100
ID: 13739086
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.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Loops Section Overview
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question