How to list directories and files

This topic describe show to list directories and files together and separately.

The list of a directory's contents can obtained by using two variants of the function RFs::GetDir(). Both variants provide a filtered list of files and directories; the second additionally provides a separate list containing directories only. Both variants use an attribute mask to filter the entry types.

The following sections assume that some directories and a file have been created in a directory FileserverExample, and then lists them in alphabetical order.

Listing directories and files together

  1. Declare a CDir pointer. This type holds an array of directory entries.

  2. Use GetDir() to get a single list, in the CDir object, which can contain both file and directory entries.

    Use KEntryAttMaskSupported to indicate all five file attributes, and the directory attribute.

    Use ESortByName to order the entries alphabetically.

  3. CDir::operator[]() can be used to get the attributes, name, size and modification date and time of the TEntry at the specified index within the array.

  4. Delete the CDir object after use.


_LIT(KDir,"\\FileserverExample\\*");
CDir* dirList;
User::LeaveIfError(fsSession.GetDir(KDir,
        KEntryAttMaskSupported,ESortByName,dirList));
_LIT(KString,"%S");
for (TInt i=0;i<dirList->Count();i++)
    console->Printf(KString,&(*dirList)[i].iName);
delete dirList;

Notes

  • GetDir() supports the use of the two wildcard characters: * (meaning any sequence of characters within any part of a component), and ? (meaning any single character).

Listing directories and files separately

If you want files and directories to be listed separately, another variant of GetDir() should be used. On return, the first list contains only files, the second directories.

  1. Use GetDir() to provide a list of directories and files and additionally a separate list of directories only.

  2. If the attribute mask KEntryAttNormal is used, no system or hidden files or directories are included in the file list.

CDir* fileList;
User::LeaveIfError(fsSession.GetDir(KDir,
        KEntryAttNormal,ESortByName,fileList,dirList));