Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find a File

Status
Not open for further replies.

shetlandbob

Programmer
Mar 9, 2004
528
GB
Hi, I'm not sure how to start this one, so any pointers appreciated.

I have a base/home directory say

C:/MyDir

And in this there is a number of sub directorys say,

C:/MyDir/one/
C:/MyDir/two/
C:/MyDir/three/
C:/MyDir/four/

Unfortunately due to reasons outwith my control these sub directorys are not using a sensible naming convention (i'm still not happy about it).

In all these directorys there is files of the format

####.XLS

where #### is a number.

is there an easy way that I can auto search (i.e. find file */*) all sub directorys until I find my file, and return the path.

The way this works is the user sets the base path ( i.e. C:/MyDir ), and then in a text box enters the number and then presses "LOAD". To load the file.

Any help pointers much appreciated.
 
Its ok I figured it our.

Here's the code for any future reference (and yes I'm sure there is many other ways to do it, but this appears to work!! Although if you know of a much faster way (this isn't slow) then I'd appreaciate it)

Code:
CString resultsDir; // defined in header

CString FileIO::FindFile ( LPCTSTR baseDirectory, CString file )
{
  CFileFind finder;

   // build a string with wildcards
  CString strWildcard( baseDirectory );
  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
    CString str = finder.GetFilePath();
    if (finder.IsDirectory())
    {
      FindFile( str, file );
    }
    else 
    {
      CFile getFileName;
      getFileName.SetFilePath ( str );
      str = getFileName.GetFileName();
      if ( str == file ) resultsDir = finder.GetFilePath();
    }
  }
  finder.Close();
  return ( resultsDir );
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top