Click here to Skip to main content
Licence 
First Posted 20 Feb 2002
Views 179,827
Bookmarked 29 times

Delete folders, subfolders and files easily

By | 20 Feb 2002 | Article
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

About the Author

Jonathan Pouliot



Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalthank you, I used it on Win CE [modified] Pinmemberjuankre10:22 2 Oct '11  
GeneralMultiple File Creation/Deletion PinmemberRockSai6:03 30 Nov '08  
GeneralCannot delete folder Pinmemberatans16:30 9 Jul '07  
GeneralRe: Cannot delete folder Pinmemberatans18:16 9 Jul '07  
Generalone condition is missing Pinmemberbjdodo5:12 7 Feb '07  
QuestionWhy its not deleting the folders which contsins files? PinmemberRaoSanthosh3:21 7 Sep '06  
GeneralI get an infinte loop with this code Pinmemberhaleyana7:03 24 May '04  
GeneralRe: I get an infinte loop with this code Pinmemberandyvinc8:03 24 May '04  
GeneralRe: I get an infinte loop with this code PinsussAnonymous3:27 20 May '05  
GeneralDeleting file folder after reboot PinmemberBalkrishna Talele20:55 20 Apr '04  
GeneralRemoveDirectory() not found Pinmembersatya19755:45 6 Nov '03  
GeneralRe: RemoveDirectory() not found PinmemberRavi Bhavnani6:29 6 Nov '03  
GeneralI have found it Pinmembersatya19756:52 6 Nov '03  
GeneralDeleteFile() may fail. PinsussAnonymous9:20 9 Dec '02  
GeneralFinally one that works! PinmemberAnonymous2:36 26 Feb '02  
GeneralRe: Finally one that works! PinmemberAnonymous3:51 16 May '02  
GeneralRe: Finally one that works! PinmemberFess66221:50 19 Feb '04  
GeneralRe: Finally one that works! Pinmembersfeldi22:03 1 Mar '04  
GeneralRe: Finally one that works! PinsussAnonymous2:27 11 Oct '05  
QuestionDeleted files ----> recycle bin ??? PinmemberAnonymous0:14 26 Feb '02  
AnswerRe: Deleted files ----> recycle bin ??? PinmemberMustafa Demirhan20:36 27 Feb '02  
Generalthanks PinmemberMazdak23:13 21 Feb '02  
QuestionIs this the easy way???? PinmemberMustafa Demirhan16:46 21 Feb '02  
AnswerRe: Is this the easy way???? PinmemberBLaZiNiX16:52 21 Feb '02  
GeneralRe: Is this the easy way???? PinmemberMustafa Demirhan17:02 21 Feb '02  

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.

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