Click here to Skip to main content
15,885,767 members
Articles / Desktop Programming / MFC
Article

Delete folders, subfolders and files easily

Rate me:
Please Sign up or sign in to vote.
4.31/5 (12 votes)
20 Feb 2002 259.1K   30   46
This article shows you how to delete all the files and subfolders in a selected folder

Introduction

I Created this to show you how to delete all the files and subfolders in a selected folder including subfolders. It's very easy to understand and it's all by using the MFC (CFileFind, with some API functions)

void RecursiveDelete(CString szPath)
{
	CFileFind ff;
	CString path = szPath;
	
	if(path.Right(1) != "\\")
		path += "\\";

	path += "*.*";

	BOOL res = ff.FindFile(path);

	while(res)
	{
		res = ff.FindNextFile();
		if (!ff.IsDots() && !ff.IsDirectory())
			DeleteFile(ff.GetFilePath());
		else if (ff.IsDirectory())
		{
			path = ff.GetFilePath();
			RecursiveDelete(path);
			RemoveDirectory(path);
		}
	}
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: Is this the easy way???? Pin
Kin Hoon19-Apr-02 19:40
Kin Hoon19-Apr-02 19:40 
GeneralRe: Is this the easy way???? Pin
Mustafa Demirhan19-Apr-02 20:02
Mustafa Demirhan19-Apr-02 20:02 
GeneralRe: Is this the easy way???? Pin
Eric Forget10-Jul-02 10:30
Eric Forget10-Jul-02 10:30 
AnswerRe: Is this the easy way???? Pin
DanPetitt6-Aug-02 11:13
DanPetitt6-Aug-02 11:13 
GeneralRe: Is this the easy way???? Pin
Mustafa Demirhan6-Aug-02 11:15
Mustafa Demirhan6-Aug-02 11:15 
GeneralRe: Is this the easy way???? Pin
DanPetitt6-Aug-02 11:20
DanPetitt6-Aug-02 11:20 
AnswerRe: Is this the easy way???? Pin
Wade H.10-Nov-02 20:05
Wade H.10-Nov-02 20:05 
GeneralRe: Is this the easy way???? Pin
Mustafa Demirhan11-Nov-02 18:20
Mustafa Demirhan11-Nov-02 18:20 
I have no idea why it does not work. You can get more information at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shfileoperation.asp[^]

Mustafa Demirhan
http://www.macroangel.com
Sonork ID 100.9935:zoltrix

<nobr>They say I'm lazy but it takes all my time
GeneralRe: Is this the easy way???? Pin
ahmad_ali13-Jan-03 3:05
ahmad_ali13-Jan-03 3:05 
GeneralRe: Is this the easy way???? Pin
Bhikshapathi Gorantla24-Feb-03 23:19
Bhikshapathi Gorantla24-Feb-03 23:19 
GeneralRe: Is this the easy way???? Pin
ahmad_ali4-Mar-03 8:26
ahmad_ali4-Mar-03 8:26 
Generalhwnd parameter Pin
Abraxas2318-Dec-03 9:14
Abraxas2318-Dec-03 9:14 
AnswerRe: Is this the easy way???? Pin
Member 10481478-Apr-05 13:28
Member 10481478-Apr-05 13:28 
GeneralRe: Is this the easy way???? Pin
fjolnir1-Jun-05 11:57
fjolnir1-Jun-05 11:57 
AnswerMessage Closed Pin
11-Apr-16 23:23
Member 1245363811-Apr-16 23:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.