If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
ashish
Earned a small fee
Posts: 14 Joined: Thu May 26, 2005 4:15 pm
Post
by ashish » Tue Aug 23, 2005 3:14 pm
Code: Select all
bool CMyClass::DeleteDirectory(wxString filename)
{
bool dir_empty = false;
{
wxDir dir(filename);
if ( !dir.IsOpened() )
{
// deal with the error here - wxDir would already log an error message
// explaining the exact reason of the failure
return FALSE;
}
wxString file;
bool cont = dir.GetFirst(&file);
file = filename + wxFileName::GetPathSeparator()+ file;
while ( cont )
{
if(wxFileName::FileExists(file)) wxRemoveFile(file);
if( wxFileName::DirExists(file)) DeleteDirectory(file);
cont = dir.GetNext(&file);
file = filename + wxFileName::GetPathSeparator()+ file;
}
if(!(dir.HasFiles() || dir.HasSubDirs()))dir_empty = true;
}
if(dir_empty){
bool error = wxRmdir(filename);
return TRUE;
}
return FALSE;
}
ssigala
Earned some good credits
Posts: 109 Joined: Fri Sep 03, 2004 9:30 am
Location: Brescia, Italy
Post
by ssigala » Tue Aug 23, 2005 3:42 pm
Good work ashish!
P.S. maybe the while loop can be reworked a bit like this:
Code: Select all
wxString file;
bool cont = dir.GetFirst(&file);
while ( cont )
{
file = filename + wxFileName::GetPathSeparator()+ file;
if(wxFileName::FileExists(file)) wxRemoveFile(file);
if( wxFileName::DirExists(file)) DeleteDirectory(file);
cont = dir.GetNext(&file);
}
vdell
Ultimate wxWidgets Guru
Posts: 536 Joined: Fri Jan 07, 2005 3:44 pm
Location: Finland
Contact:
Post
by vdell » Tue Aug 23, 2005 4:00 pm
Visual C++ 9.0 / Windows XP Pro SP3 / wxWidgets 2.9.0 (SVN) |
Colligere
Jorg
Moderator
Posts: 3971 Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:
Post
by Jorg » Tue Aug 23, 2005 8:26 pm
Minor suggestion, use the wxFileName::AppendDir function instead of dealing with the path seperator. The AppendDir will deal with it for you. By using a wxFileName to begin with (instead of wxString) you do not have to concatenate all the time
But besides that it's another useful contribution to the Code Dump, thanks!
- Jorgen