Alejandro
2009-05-04 14:39:26 UTC
Hi All,
I made a function to remove directories ("MyRemoveDir(const
wxFileName)"). I made an implementation of wxDirTraverser to enumerates
the directories to remove.
In the function MyRemoveDir, create a wxDir with the path of the
directory to remove(which is the parameter of the function), to invocate
wxDir::Traverse and enumerate subdirectories. Then when I try to remove
directories with the function wxRmdir() it fails, like when you try to
delete a file that is open.
I've seen that when a wxDir is created, it creates a struct(wxDirData)
and I think that this is the cause because wxRmdir fails(in the first
case). On the other hand wxDir help says: "wxDir is a portable
equivalent of Unix open/read/closedir functions..." but I couldn't close
a directory and wxDir isn't doing.
This is the code:
bool MyRemoveDir(const wxFileName &FnDir)
{
wxArrayString dirs;
wxDirTraverserListDirs traverser(dirs);
wxDir dir(FnDir.GetFullPath());
size_t count = dir.Traverse(traverser);
//....delete files to make empty the directories...
for(size_t i=0; i < dirs.Count(); i++)
if (!MyRemoveDir(wxFileName(dirs.Item(i),wxT(""))))
show_error_msg;
return wxRmdir(FnDir.GetPathWithSep().c_str()); //this fails
}
To fix it, I made this:
bool MyRemoveDir(const wxFileName &FnDir)
{
...like above...
//This is the change
wxDir *pdir = new wxDir(FnDir.GetFullPath());
size_t count = pdir->Traverse(traverser);
delete pdir;
like above
}
Is correct the second implementation or wxDir must provide a method to
close the directories?
Thanks.
Alejandro.
I made a function to remove directories ("MyRemoveDir(const
wxFileName)"). I made an implementation of wxDirTraverser to enumerates
the directories to remove.
In the function MyRemoveDir, create a wxDir with the path of the
directory to remove(which is the parameter of the function), to invocate
wxDir::Traverse and enumerate subdirectories. Then when I try to remove
directories with the function wxRmdir() it fails, like when you try to
delete a file that is open.
I've seen that when a wxDir is created, it creates a struct(wxDirData)
and I think that this is the cause because wxRmdir fails(in the first
case). On the other hand wxDir help says: "wxDir is a portable
equivalent of Unix open/read/closedir functions..." but I couldn't close
a directory and wxDir isn't doing.
This is the code:
bool MyRemoveDir(const wxFileName &FnDir)
{
wxArrayString dirs;
wxDirTraverserListDirs traverser(dirs);
wxDir dir(FnDir.GetFullPath());
size_t count = dir.Traverse(traverser);
//....delete files to make empty the directories...
for(size_t i=0; i < dirs.Count(); i++)
if (!MyRemoveDir(wxFileName(dirs.Item(i),wxT(""))))
show_error_msg;
return wxRmdir(FnDir.GetPathWithSep().c_str()); //this fails
}
To fix it, I made this:
bool MyRemoveDir(const wxFileName &FnDir)
{
...like above...
//This is the change
wxDir *pdir = new wxDir(FnDir.GetFullPath());
size_t count = pdir->Traverse(traverser);
delete pdir;
like above
}
Is correct the second implementation or wxDir must provide a method to
close the directories?
Thanks.
Alejandro.