Click here to Skip to main content
15,893,588 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.4K   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

 
GeneralDeleting file folder after reboot Pin
Balkrishna Talele20-Apr-04 20:55
Balkrishna Talele20-Apr-04 20:55 
GeneralRemoveDirectory() not found Pin
satya19756-Nov-03 5:45
satya19756-Nov-03 5:45 
GeneralRe: RemoveDirectory() not found Pin
Ravi Bhavnani6-Nov-03 6:29
professionalRavi Bhavnani6-Nov-03 6:29 
GeneralI have found it Pin
satya19756-Nov-03 6:52
satya19756-Nov-03 6:52 
GeneralDeleteFile() may fail. Pin
Anonymous9-Dec-02 9:20
Anonymous9-Dec-02 9:20 
GeneralFinally one that works! Pin
26-Feb-02 2:36
suss26-Feb-02 2:36 
GeneralRe: Finally one that works! Pin
16-May-02 3:51
suss16-May-02 3:51 
GeneralRe: Finally one that works! Pin
Fess66219-Feb-04 21:50
Fess66219-Feb-04 21:50 
GeneralRe: Finally one that works! Pin
sfeldi1-Mar-04 22:03
sfeldi1-Mar-04 22:03 
GeneralRe: Finally one that works! Pin
Anonymous11-Oct-05 2:27
Anonymous11-Oct-05 2:27 
QuestionDeleted files ----> recycle bin ??? Pin
26-Feb-02 0:14
suss26-Feb-02 0:14 
AnswerRe: Deleted files ----> recycle bin ??? Pin
Mustafa Demirhan27-Feb-02 20:36
Mustafa Demirhan27-Feb-02 20:36 
Generalthanks Pin
Mazdak21-Feb-02 23:13
Mazdak21-Feb-02 23:13 
QuestionIs this the easy way???? Pin
Mustafa Demirhan21-Feb-02 16:46
Mustafa Demirhan21-Feb-02 16:46 
AnswerRe: Is this the easy way???? Pin
BLaZiNiX21-Feb-02 16:52
BLaZiNiX21-Feb-02 16:52 
GeneralRe: Is this the easy way???? Pin
Mustafa Demirhan21-Feb-02 17:02
Mustafa Demirhan21-Feb-02 17:02 
AnswerRe: Is this the easy way???? Pin
Yury Goltsman23-Feb-02 22:34
Yury Goltsman23-Feb-02 22:34 
GeneralRe: Is this the easy way???? Pin
Mustafa Demirhan24-Feb-02 7:42
Mustafa Demirhan24-Feb-02 7:42 
AnswerRe: Is this the easy way???? Pin
Philippe Lhoste27-Feb-02 2:09
Philippe Lhoste27-Feb-02 2:09 
GeneralRe: Is this the easy way???? Pin
Mustafa Demirhan27-Feb-02 5:56
Mustafa Demirhan27-Feb-02 5:56 
AnswerRe: Is this the easy way???? Pin
Tony Belcher7-Mar-02 14:37
Tony Belcher7-Mar-02 14:37 
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 

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.