How to search for files with TFindFile

This topic provides an example on how to search for files with TFindFile.

The following example accumulates a list of all the files on any drive which are in a particular directory and which match a name with wildcards, for example, all files matching \resources\fonts\*.gdr .

To start a search, use TFindFile::FindWildByDir() . You can then call TFindFile::FindWild() to perform the same search on another drive.

To retrieve the fully qualified path of the matching files, class TParse is used to combine the filename with the drive letter and directory which contains the file. The example works as follows:

  1. Construct a TFindFile object.

  2. Use FindWildByDir() to start the search for matching files. There is considerable flexibility in the handling of aWildName and aScanDir , but the simplest and most common case is where aWildName is the filename and extension (for example, *.gdr ) and aScanDir is the directory name, without a drive letter, but including a trailing directory separator (for example, \resources\fonts\ ).

  3. The list of matching files is returned in a CDir object, which is implemented as an array. Count() retrieves the number of items in the list.

  4. Use file_finder.File() to retrieve the drive and path of the folder containing the files in the CDir , (for example Z:\resources\fonts\ ).

  5. Use TParse::Set() to combine the file name and path into a full filename.

  6. Use TFindFile::FindWild() to continue the search on the next drive in the search sequence.

      
       
      
      void ForAllMatchingFiles(RFs& aSession, const TDesC& aWildName, 
            const TDesC& aScanDir)
    {        
    TFindFile file_finder(aSession);
    CDir* file_list;        
    TInt err = file_finder.FindWildByDir(aWildname,aScanDir, file_list);
    while (err==KErrNone)
        {
        TInt i;
        for (i=0; i<file_list->Count(); i++)
            {
            TParse fullentry;
            fullentry.Set((*file_list)[i].iName,& file_finder.File(),NULL);
            // Do something with the full filename...
            // ...
            }
        delete file_list;
        err=file_finder.FindWild(file_list);
        }
    }
     

Notes

  • (*file_list)[i].iName is the name of a file matching the pattern specified (e.g. Eon.gdr ).

  • It is your responsibility to delete the CDir object when you have finished with it.