Contents Up Previous Next

wxDir

wxDir is a portable equivalent of Unix open/read/closedir functions which allow enumerating of the files in a directory. wxDir allows enumerate files as well as directories.

wxDir also provides a flexible way to enumerate files recursively using Traverse or a simpler GetAllFiles function.

Example of use:

    wxDir dir(wxGetCwd());

    if ( !dir.IsOpened() )
    {
        // deal with the error here - wxDir would already log an error message
        // explaining the exact reason of the failure
        return;
    }

    puts("Enumerating object files in current directory:");

    wxString filename;

    bool cont = dir.GetFirst(&filename, filespec, flags);
    while ( cont )
    {
        printf("%s\n", filename.c_str());

        cont = dir.GetNext(&filename);
    }
Derived from

No base class

Constants

These flags define what kind of filename is included in the list of files enumerated by GetFirst/GetNext.

enum
{
    wxDIR_FILES     = 0x0001,       // include files
    wxDIR_DIRS      = 0x0002,       // include directories
    wxDIR_HIDDEN    = 0x0004,       // include hidden files
    wxDIR_DOTDOT    = 0x0008,       // include '.' and '..'

    // by default, enumerate everything except '.' and '..'
    wxDIR_DEFAULT   = wxDIR_FILES | wxDIR_DIRS | wxDIR_HIDDEN
}

Include files

<wx/dir.h>

Members

wxDir::wxDir
wxDir::~wxDir
wxDir::Exists
wxDir::GetAllFiles
wxDir::GetFirst
wxDir::GetName
wxDir::GetNext
wxDir::HasFiles
wxDir::HasSubDirs
wxDir::IsOpened
wxDir::Open
wxDir::Traverse


wxDir::wxDir

wxDir()

Default constructor, use Open() afterwards.

wxDir(const wxString& dir)

Opens the directory for enumeration, use IsOpened() to test for errors.


wxDir::~wxDir

~wxDir()

Destructor cleans up the associated resources. It is not virtual and so this class is not meant to be used polymorphically.


wxDir::Exists

static bool Exists(const wxString& dir)

Test for existence of a directory with the given name


wxDir::GetAllFiles

static size_t GetAllFiles(const wxString& dirname, wxArrayString *files, const wxString& filespec = wxEmptyString, int flags = wxDIR_DEFAULT)

The function appends the names of all the files under directory dirname to the array files (note that its old content is preserved). Only files matching the filespec are taken, with empty spec matching all the files.

The flags parameter should always include wxDIR_FILES or the array would be unchanged and should include wxDIR_DIRS flag to recurse into subdirectories (both flags are included in the value by default).

See also: Traverse


wxDir::GetFirst

bool GetFirst(wxString* filename, const wxString& filespec = wxEmptyString, int flags = wxDIR_DEFAULT) const

Start enumerating all files matching filespec (or all files if it is empty) and flags, return true on success.


wxDir::GetName

wxString GetName() const

Returns the name of the directory itself. The returned string does not have the trailing path separator (slash or backslash).


wxDir::GetNext

bool GetNext(wxString* filename) const

Continue enumerating files satisfying the criteria specified by the last call to GetFirst.


wxDir::HasFiles

bool HasFiles(const wxString& filespec = wxEmptyString)

Returns true if the directory contains any files matching the given filespec. If filespec is empty, look for any files at all. In any case, even hidden files are taken into account.


wxDir::HasSubDirs

bool HasSubDirs(const wxString& dirspec = wxEmptyString)

Returns true if the directory contains any subdirectories (if a non empty filespec is given, only check for directories matching it). The hidden subdirectories are taken into account as well.


wxDir::IsOpened

bool IsOpened() const

Returns true if the directory was successfully opened by a previous call to Open.


wxDir::Open

bool Open(const wxString& dir)

Open the directory for enumerating, returns true on success or false if an error occurred.


wxDir::Traverse

size_t Traverse(wxDirTraverser& sink, const wxString& filespec = wxEmptyString, int flags = wxDIR_DEFAULT)

Enumerate all files and directories under the given directory recursively calling the element of the provided wxDirTraverser object for each of them.

More precisely, the function will really recurse into subdirectories if flags contains wxDIR_DIRS flag. It will ignore the files (but still possibly recurse into subdirectories) if wxDIR_FILES flag is given.

For each found directory, sink.OnDir() is called and sink.OnFile() is called for every file. Depending on the return value, the enumeration may continue or stop.

The function returns the total number of files found or (size_t)-1 on error.

See also: GetAllFiles