how to use wxdir class? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
bartsob5
Knows some wx things
Knows some wx things
Posts: 41
Joined: Mon Mar 02, 2009 10:26 pm
Location: Poland

how to use wxdir class?

Post by bartsob5 » Wed Mar 18, 2009 9:18 pm

hi...

this time i have problem like this:
i need a list of files which are stored in directory (and all subdirectories), however i don't want to include system's directories..
my project is only for windows (if that matters) and i used to get that list by making a command line file (the one with .cmd extantion) with written something like:

Code: Select all

 dir [path] /A: -D /S /B /O:E 
and executed it with wxexecute, which is fine, but only for small folders (less than 5 000 files) and that freezing when executing cmd is somewhat strange.
anyway, i wanted to make some limitations, that user can't choose system folders (well, not that he can't choose, but the program should ignore such folders, so when the user chooses directory c:\ it will give me a list of all files on this partition, except for those which are stored in c:\windows or c:\program files).
my problem is that i don't fully understand manual...
expecially that 'virtual/abstract class' thing (i mean wxdirtraverse)
can you point me the way how to get a list of files in specified path? (i think that dirtraverse class confuses me the most)

mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r » Thu Mar 19, 2009 5:09 am

The example here doesn't require a wxDirTraverser.

Use the functions here to extend it. Check to see if the filename is in your list of "system" directories that should be excluded. If so exclude it. Otherwise check to see if it is a file or a dir. If it is dir, recursivly call the function you are currently in passing it the new dir as the directory to be processed. If it is a file, do whatever you want done with a file.

FYI, a virtual class means it is a class with a virtual function or functions. A virtual function is a function in a parent class which can be overloaded by a child of that parent to provide new functionality.

An abstract or abastract virtual class is on with an abstract virtual function. This is a virtual function that is declared for the parent but no definition is provided. Because of the missing definition you can't create an instance of the parent class but only of a child class that provides its own definition for the missing function.

These are to key topics for C++. I would recommend reading up on polymorphism as it will greatly help you with your C++ code and wxWidgets as it is based on the concept. ie... a wxTextCtrl ISA wxControl ISA wxPanel ISA wxWindow ISA wxEvtHandler ISA wxObject.

-Max

bartsob5
Knows some wx things
Knows some wx things
Posts: 41
Joined: Mon Mar 02, 2009 10:26 pm
Location: Poland

Post by bartsob5 » Thu Mar 19, 2009 6:00 pm

hmmm, that example seems to work for me, but still, i'm not exactly sure what should i put in a place of 'filespec'. i only found, that there can be wxEmptyString, but it doesn't work in the way i'd like (for instance, i'd like to get files subdirectories as well). Looking through the function list i can come up with an idea, that i can check if there is any subdirectory and set another working directory. but still, i can't find any function which would return me the name of subdirectory, and i actually don't have any idea, of how to get files if there is more than one subdirectory...

Code: Select all

wxString filename,path;

    bool cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_FILES|wxDIR_DIRS|wxDIR_HIDDEN  );
    path=dir.GetName();
    while ( cont )
    {
        //probably this is the place, where i want to do the 
        //limitations of system directory


        WxMemo1->AppendText(path);
        WxMemo1->AppendText(filename.c_str());

        WxMemo1->AppendText("\n");
        path.Clear();
        cont = dir.GetNext(&filename);
        path=dir.GetName();
        if(dir.hasSubDirs(wxEmptyString)==true)
        {
          //i should get here the name of the subdirectory
          //and put it into 'dir'
          //but what shall i do when there is more than 
         //subdirectory?
        }
    }


mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r » Thu Mar 19, 2009 6:10 pm

bartsob5 wrote:hmmm, that example seems to work for me, but still, i'm not exactly sure what should i put in a place of 'filespec'. i only found, that there can be wxEmptyString, but it doesn't work in the way i'd like (for instance, i'd like to get files subdirectories as well). Looking through the function list i can come up with an idea, that i can check if there is any subdirectory and set another working directory. but still, i can't find any function which would return me the name of subdirectory, and i actually don't have any idea, of how to get files if there is more than one subdirectory...
Thats what the second link was for. Use the functions there to test the names returned to see if the are files or directories.

If it is a directory you process it the same as the original(root) directory with wxDir.

-Max

bartsob5
Knows some wx things
Knows some wx things
Posts: 41
Joined: Mon Mar 02, 2009 10:26 pm
Location: Poland

Post by bartsob5 » Tue Mar 24, 2009 5:59 pm

it seems like wxGetAllFiles worked for me here... maybe it won't be optimized in perfect way, but it works.

Post Reply