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 to 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::findfirst
wxdir::getfirst
wxdir::getname
wxdir::getnext
wxdir::gettotalsize
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::findfirst

static wxstring findfirst(const wxstring& dirname, const wxstring& filespec, int flags = wxdir_default)

the function returns the path of the first file matching the given filespec or an empty string if there are no files matching it.

the flags parameter may or may not include wxdir_files, the function always behaves as if it were specified. by default, flags includes wxdir_dirs and so the function recurses into the subdirectories but if this flag is not specified, the function restricts the search only to the directory dirname itself.

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 which satisfy the criteria specified by the last call to getfirst.


wxdir::gettotalsize

static wxulonglong gettotalsize(const wxstring& dir, wxarraystring* filesskipped = null)

returns the size (in bytes) of all files recursively found in dir or wxinvalidsize in case of error.

in case it happens that while traversing folders a file's size can not be read, that file is added to the filesskipped array, if not null, and then skipped. this usually happens with some special folders which are locked by the operating system or by another process. remember that when filesskipped->getcount() is not zero, then the returned value is not 100% accurate and, if the skipped files were big, it could be far from real size of the directory.

see also: wxfilename::gethumanreadablesize, wxgetdiskspace


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