Click here to Skip to main content
11,645,349 members (67,245 online)
Click here to Skip to main content

Delete folders, subfolders and files easily

, 20 Feb 2002 207K 29
Rate this:
Please Sign up or sign in to vote.
This article shows you how to delete all the files and subfolders in a selected folder
<!-- Add the rest of your HTML here -->

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

Share

About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Rahul (Software Engineer)28-Sep-12 3:07
memberRahul (Software Engineer)28-Sep-12 3:07 
Generalthank you, I used it on Win CE Pin
juankre2-Oct-11 10:22
memberjuankre2-Oct-11 10:22 
GeneralMultiple File Creation/Deletion Pin
RockSai30-Nov-08 6:03
memberRockSai30-Nov-08 6:03 
GeneralCannot delete folder Pin
atans9-Jul-07 16:30
memberatans9-Jul-07 16:30 
GeneralRe: Cannot delete folder Pin
atans9-Jul-07 18:16
memberatans9-Jul-07 18:16 
Generalone condition is missing Pin
bjdodo7-Feb-07 5:12
memberbjdodo7-Feb-07 5:12 
QuestionWhy its not deleting the folders which contsins files? Pin
RaoSanthosh7-Sep-06 3:21
memberRaoSanthosh7-Sep-06 3:21 
GeneralI get an infinte loop with this code Pin
haleyana24-May-04 7:03
memberhaleyana24-May-04 7:03 
GeneralRe: I get an infinte loop with this code Pin
andyvinc24-May-04 8:03
memberandyvinc24-May-04 8:03 
GeneralRe: I get an infinte loop with this code Pin
Anonymous20-May-05 3:27
sussAnonymous20-May-05 3:27 
GeneralDeleting file folder after reboot Pin
Balkrishna Talele20-Apr-04 20:55
memberBalkrishna Talele20-Apr-04 20:55 
GeneralRemoveDirectory() not found Pin
satya19756-Nov-03 5:45
membersatya19756-Nov-03 5:45 
GeneralRe: RemoveDirectory() not found Pin
Ravi Bhavnani6-Nov-03 6:29
memberRavi Bhavnani6-Nov-03 6:29 
GeneralI have found it Pin
satya19756-Nov-03 6:52
membersatya19756-Nov-03 6:52 
GeneralDeleteFile() may fail. Pin
Anonymous9-Dec-02 9:20
sussAnonymous9-Dec-02 9:20 
GeneralFinally one that works! Pin
Anonymous26-Feb-02 2:36
memberAnonymous26-Feb-02 2:36 
GeneralRe: Finally one that works! Pin
Anonymous16-May-02 3:51
memberAnonymous16-May-02 3:51 
GeneralRe: Finally one that works! Pin
Fess66219-Feb-04 21:50
memberFess66219-Feb-04 21:50 
GeneralRe: Finally one that works! Pin
sfeldi1-Mar-04 22:03
membersfeldi1-Mar-04 22:03 
GeneralRe: Finally one that works! Pin
Anonymous11-Oct-05 2:27
sussAnonymous11-Oct-05 2:27 
QuestionDeleted files ----> recycle bin ??? Pin
Anonymous26-Feb-02 0:14
memberAnonymous26-Feb-02 0:14 
AnswerRe: Deleted files ----> recycle bin ??? Pin
Mustafa Demirhan27-Feb-02 20:36
memberMustafa Demirhan27-Feb-02 20:36 
Generalthanks Pin
Mazdak21-Feb-02 23:13
memberMazdak21-Feb-02 23:13 
QuestionIs this the easy way???? Pin
Mustafa Demirhan21-Feb-02 16:46
memberMustafa Demirhan21-Feb-02 16:46 
AnswerRe: Is this the easy way???? Pin
BLaZiNiX21-Feb-02 16:52
memberBLaZiNiX21-Feb-02 16:52 
GeneralRe: Is this the easy way???? Pin
Mustafa Demirhan21-Feb-02 17:02
memberMustafa Demirhan21-Feb-02 17:02 
AnswerRe: Is this the easy way???? Pin
Yury Goltsman23-Feb-02 22:34
memberYury Goltsman23-Feb-02 22:34 
GeneralRe: Is this the easy way???? Pin
Mustafa Demirhan24-Feb-02 7:42
memberMustafa Demirhan24-Feb-02 7:42 
AnswerRe: Is this the easy way???? Pin
Philippe Lhoste27-Feb-02 2:09
memberPhilippe Lhoste27-Feb-02 2:09 
GeneralRe: Is this the easy way???? Pin
Mustafa Demirhan27-Feb-02 5:56
memberMustafa Demirhan27-Feb-02 5:56 
AnswerRe: Is this the easy way???? Pin
Tony Belcher7-Mar-02 14:37
memberTony Belcher7-Mar-02 14:37 
AnswerRe: Is this the easy way???? Pin
Kin Hoon19-Apr-02 19:40
memberKin Hoon19-Apr-02 19:40 
GeneralRe: Is this the easy way???? Pin
Mustafa Demirhan19-Apr-02 20:02
memberMustafa Demirhan19-Apr-02 20:02 
GeneralRe: Is this the easy way???? Pin
Eric Forget10-Jul-02 10:30
memberEric Forget10-Jul-02 10:30 
AnswerRe: Is this the easy way???? Pin
DanPetitt6-Aug-02 11:13
memberDanPetitt6-Aug-02 11:13 
GeneralRe: Is this the easy way???? Pin
Mustafa Demirhan6-Aug-02 11:15
memberMustafa Demirhan6-Aug-02 11:15 
GeneralRe: Is this the easy way???? Pin
DanPetitt6-Aug-02 11:20
memberDanPetitt6-Aug-02 11:20 
AnswerRe: Is this the easy way???? Pin
Wade H.10-Nov-02 20:05
memberWade H.10-Nov-02 20:05 
GeneralRe: Is this the easy way???? Pin
Mustafa Demirhan11-Nov-02 18:20
memberMustafa Demirhan11-Nov-02 18:20 
GeneralRe: Is this the easy way???? Pin
ahmad_ali13-Jan-03 3:05
memberahmad_ali13-Jan-03 3:05 
GeneralRe: Is this the easy way???? Pin
Pathi Gorantla24-Feb-03 23:19
memberPathi Gorantla24-Feb-03 23:19 
GeneralRe: Is this the easy way???? Pin
ahmad_ali4-Mar-03 8:26
memberahmad_ali4-Mar-03 8:26 
Generalhwnd parameter Pin
Abraxas2318-Dec-03 9:14
memberAbraxas2318-Dec-03 9:14 
AnswerRe: Is this the easy way???? Pin
Florent95968-Apr-05 13:28
memberFlorent95968-Apr-05 13:28 
GeneralRe: Is this the easy way???? Pin
fjolnir1-Jun-05 11:57
memberfjolnir1-Jun-05 11:57 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 21 Feb 2002
Article Copyright 2002 by Jonathan Pouliot
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid