Removing a Directory

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!
Post Reply
Tyler
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 246
Joined: Fri Sep 03, 2004 12:37 am
Contact:

Removing a Directory

Post by Tyler » Fri Jun 03, 2005 5:09 pm

I'm sorry Jorgen, I had to do it. Per our previous discussion at: http://forums.wxwidgets.org/viewtopic.php?t=2118

I took goeba's correction of my function from: http://forums.wxwidgets.org/viewtopic.php?t=2080
and tweaked it into a remove directory function. I added a warning to the comments and also a safety check to hopefully keep people from doing something they shouldn't.

Code: Select all

// =================================================================
//  *** WARNING ***
//  Do not misuse this function.  It will delete the directory you 
//  pass it without asking or double checking with you.  In other
//  words, you could delete your entire hard drive if you are not 
//  careful.  I have added a safety precaution block which you can
//  comment out.  It should prevent you from deleting "c:\" on 
//  windows, but I assume no responsibility for your loss of data.
// =================================================================
//
//	Date:			06/03/05
//	Platforms:		Written and tested under Win XP
//  Compiler:		MSVC++ 6.0
//	wxWidgets:		Tested under 2.6.0, but should work in others.
//
//	Description:	Equivalent to the unix "rm -rf" command.  Will 
//					remove a directory and all of it's contents 
//					(recursively).
//
//  Resources:		Code produced from modified version of CopyDir 
//					function availble at:  
//					http://wxforum.shadonet.com/viewtopic.php?t=2080
// 
//  Example Usage:  
//
//					RemDirRF("C:\\testDir");
//
//	Returns:		True if successful, false otherwise.
// =================================================================
bool RemDirRF(wxString rmDir) {

	// *************************************************************
	// COMMENT THIS BLOCK OUT IF YOU WANT TO BE ABLE TO DELETE ROOT
	// DIRECTORIES : IT IS RECOMMENDED YOU LEAVE IT AS IS
	// *************************************************************
	// BEGIN SAFETY CHECK
	// *************************************************************
	///*
	// if it's a possible root directory
	if (rmDir.length() <= 3) {
		wxLogError("You asked RemDirRF() to delete a possible root directory.\
  This is disabled by default.  If you really need to delete \"" +
			rmDir + "\" please alter the RemDirRF() definition.");

		return false;
	}
	//*/
	// *************************************************************
	// END SAFETY CHECK
	// *************************************************************

    // first make sure that the dir exists
    if(!wxDir::Exists(rmDir)) {
            wxLogError(rmDir + " does not exist.  Could not remove directory.");
            return false;
    }
       
	// append a slash if we don't have one
	if (rmDir[rmDir.length()-1] != wxFILE_SEP_PATH) {
		rmDir += wxFILE_SEP_PATH;       
    }

	// define our directory object.  When we begin traversing it, the
	// os will not let go until the object goes out of scope.
    wxDir* dir = new wxDir(rmDir);

	// check for allocation failure
	if (dir == NULL) {
		wxLogError("Could not allocate new memory on the heap!");
		return false;
	}

	// our file name temp var
    wxString filename;
	// get the first filename
    bool cont = dir->GetFirst(&filename);

	// if there are files to process
    if (cont){
        do {
			// if the next filename is actually a directory
            if (wxDirExists(rmDir + filename)) {
				// delete this directory
                RemDirRF(rmDir + filename);
            }
            else {
				// otherwise attempt to delete this file
                if(!wxRemoveFile(rmDir + filename)) {
					// error if we couldn't
					wxLogError("Could not remove file \"" + rmDir + filename + "\"");
				}
            }
        }
		// get the next file name
        while (dir->GetNext(&filename));
    }   

	// Remove our directory object, so the OS will let go of it and
	// allow us to delete it
	delete dir;

	// now actually try to delete it
	if (!wxFileName::Rmdir(rmDir)) {
		// error if we couldn't
		wxLogError("Could not remove directory " + rmDir);
		// return accordingly
		return false;
	}
	// otherwise
	else {
		// return that we were successfull.
		return true; 
	}

}

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton » Tue Jun 07, 2005 8:45 am

Great stuff!

Tyler
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 246
Joined: Fri Sep 03, 2004 12:37 am
Contact:

Post by Tyler » Tue Jun 07, 2005 1:14 pm

Thanks man! And thanks to goeba for laying out the function originally.

ghostd0g
Experienced Solver
Experienced Solver
Posts: 58
Joined: Wed Oct 19, 2005 3:58 pm

Post by ghostd0g » Thu Feb 23, 2006 2:15 pm

thx man. a word to the board admins. shouldn't the links to the old forum inside the threads be updated?

aashiks
In need of some credit
In need of some credit
Posts: 4
Joined: Fri Jul 14, 2006 12:54 pm
Location: Trivandrum , India

Post by aashiks » Fri Sep 08, 2006 6:15 am

Thank you very much :D I needed to modify this a bit for my purpose , but I am putting a comment in my source file saying this is where I got it from
Its what you do to others that matters. Not vice versa .

aozima
In need of some credit
In need of some credit
Posts: 5
Joined: Wed Oct 05, 2011 1:20 pm

Re: Removing a Directory

Post by aozima » Thu Oct 27, 2011 5:16 pm

very thanks! :)

Post Reply