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!

finding files

Status
Not open for further replies.

malonep

Programmer
Jul 14, 2004
44
CA
Hi,

I have a known directory c:\transferfolder\

and I would like to select all the files from that directory that have the ending "_s.txt"

Is c++ able to do this?
 
the code below searches recursively in a base directory for a specific file name, if it finds it then it returns the full path.

I'm sure this could be copied and modified to do what you want?

hint: remove the input string "file", change the wildcard to "*_s.txt", and build an array of all the files that it finds.

Code:
// in header
CString resultsDir;



CString FileIO::FindFile ( LPCTSTR baseDirectory, CString file )
{
  CFileFind finder;
  
  resultsDir = "none";  // used to check if file has not been found.
   // 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 recursively search infinitely!
    if ( finder.IsDots() ) continue;

    CString str = finder.GetFilePath();
    // if change dir name so has first run no in it - we can change the search criteria
    
    // if it's a directory, recursively search it
    if ( finder.IsDirectory() )
    {
      if ( FindFile( str, file ) != "none" ) bWorking = false; // if FindFile != "none" -> file has been found - so exit
    }
    else 
    {
      if ( finder.GetFileName() == file ) 
      { 
        bWorking = false; 
        resultsDir = finder.GetFilePath();
      }
    }
  }
  finder.Close();
  return ( resultsDir );
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top