Help please! How to get directories?

If you are using wxDev-C++ for your wxWidgets design, please ask your questions here instead of in IDE Related.
Post Reply
wei917
Earned a small fee
Earned a small fee
Posts: 12
Joined: Thu Aug 03, 2006 6:04 am

Help please! How to get directories?

Post by wei917 » Fri Aug 04, 2006 9:22 pm

I want to write a image shower like ACDSee,
I can show image by its name, but now I want to build a treectrl just like window explorer.
I tried use wxDir

First copy this class from website:

---------------------------------------------------
class wxDirTraverserSimple : public wxDirTraverser
{
public:
wxDirTraverserSimple(wxArrayString& files) : m_files(files) { }

virtual wxDirTraverseResult OnFile(const wxString& filename)
{
m_files.Add(filename);
return wxDIR_CONTINUE;
}

virtual wxDirTraverseResult OnDir(const wxString& WXUNUSED(dirname))
{
return wxDIR_CONTINUE;
}

private:
wxArrayString& m_files;
};

-------------------------------------------------

And then I do:

wxDir dir1(wxGetCwd());
wxArrayString files;
wxDirTraverserSimple traverser(files);

dir1.Traverse(traverser);


it works fine, but........... where is the wxString of directories? and all other files?

Thanks a lot!!!!!
I think as long as I solved this, my ACDwatch beta could be released.

toxicBunny
Super wx Problem Solver
Super wx Problem Solver
Posts: 424
Joined: Tue Jul 12, 2005 8:44 pm
Location: Alabama, USA

Post by toxicBunny » Fri Aug 04, 2006 9:44 pm

I've never used this class, but it looks like the information you want should be stored in the "files" variable. This is a wxArrayString and not just a normal wxString. Only files would be added to the array.

You can use

Code: Select all

files.GetCount()
to get the number files.

For information on wxArrayString, see this link.

-Scott
wxMSW 2.6.2, VS 2002, 2003 and 2005, Code::Blocks and mingw, Windows XP Pro

wei917
Earned a small fee
Earned a small fee
Posts: 12
Joined: Thu Aug 03, 2006 6:04 am

Post by wei917 » Fri Aug 04, 2006 11:13 pm

Hi,
but it only can get files.
I want the directories...how can I do ?
Thanks~~~~

benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Post by benedicte » Fri Aug 04, 2006 11:33 pm

In the "OnDir" function, store the "dirname" in a local structure.

The code you pasted is designed to store only files and traverse all dirs recursively. You can easilly modify it to store only directories and ignore files.

wei917
Earned a small fee
Earned a small fee
Posts: 12
Joined: Thu Aug 03, 2006 6:04 am

Post by wei917 » Sat Aug 05, 2006 1:44 am

Got it,

virtual wxDirTraverseResult OnDir(const wxString& dirname)
{
m_files.Add(dirname);
return wxDIR_CONTINUE;
}

But could you tell me what class you use for get the directories?

Thanks!!!!!!!

benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Post by benedicte » Sat Aug 05, 2006 9:24 am

You can derive your own class from wxDirTraverser like the sample you copied.

toxicBunny
Super wx Problem Solver
Super wx Problem Solver
Posts: 424
Joined: Tue Jul 12, 2005 8:44 pm
Location: Alabama, USA

Post by toxicBunny » Mon Aug 07, 2006 5:20 pm

Try this

Code: Select all

class wxMyDirTraverser : public wxDirTraverser 
{ 
public: 
  wxDirTraverserSimple(wxArrayString& files) : m_files(files), m_dirs(dirs) { } 

  virtual wxDirTraverseResult OnFile(const wxString& filename) 
  { 
    m_files.Add(filename); 
    return wxDIR_CONTINUE; 
  } 

  virtual wxDirTraverseResult OnDir(const wxString& dirname) 
  { 
    m_dirs.Add(dirname);
    return wxDIR_CONTINUE; 
  } 

  private: 
  wxArrayString& m_files; 
  wxArrayString& m_dirs;
};
Then call

Code: Select all

wxDir dir1(wxGetCwd()); 
wxArrayString files, dirs; 
wxMyDirTraverser traverser(files, dirs); 

dir1.Traverse(traverser);
This gives you one array with all the directories and one with all the files. Of course, I haven't compiled or tried this code, so you may have to tweak it a bit.

-Scott
wxMSW 2.6.2, VS 2002, 2003 and 2005, Code::Blocks and mingw, Windows XP Pro

tiwag
Earned some good credits
Earned some good credits
Posts: 123
Joined: Tue Dec 21, 2004 8:51 pm
Location: Austria

Post by tiwag » Mon Aug 07, 2006 6:01 pm

Hi Scott - nice idea !

after a bit *tweaking*, it really works (tested)

Code: Select all

#include <wx/wx.h>
#include <wx/dir.h>
#include <wx/arrstr.h>


class wxMyDirTraverser : public wxDirTraverser
{
public:
    wxMyDirTraverser(wxArrayString& files, wxArrayString& dirs) : m_files(files), m_dirs(dirs)
    { }

    virtual wxDirTraverseResult OnFile(const wxString& filename)
    {
        m_files.Add(filename);
        return wxDIR_CONTINUE;
    }

    virtual wxDirTraverseResult OnDir(const wxString& dirname)
    {
        m_dirs.Add(dirname);
        return wxDIR_CONTINUE;
    }

private:
    wxArrayString& m_files;
    wxArrayString& m_dirs;
};


int main(int argc, char **argv)
{
    wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");

    wxInitializer initializer;
    if ( !initializer )
    {
        fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");

        return -1;
    }


    wxDir dir1(wxGetCwd());
    wxArrayString files, dirs;
    wxMyDirTraverser traverser(files, dirs);

    dir1.Traverse(traverser);

    wxPuts(wxT("Files:"));
    for(size_t i=0; i<files.GetCount(); i++)
        wxPuts(files.Item(i));

    wxPuts(wxT("Dirs:"));
    for(size_t i=0; i<dirs.GetCount(); i++)
        wxPuts(dirs.Item(i));

}
-tiwag

Post Reply