Click here to Skip to main content
15,885,278 members
Articles / Mobile Apps / Windows Mobile
Article

Deleting a directory along with sub-folders

Rate me:
Please Sign up or sign in to vote.
2.98/5 (22 votes)
16 Dec 2004GPL3 160.7K   26   35
Function that deletes whole of a directory structure.

Introduction

First of all, let me tell you that this is my first contribution to CodeProject though I have been programming in C++ for more than five years now. Having said that, I think, I have good reasons to keep my first article short and simple for beginners.

Deleting a directory structure

The Windows API RemoveDirectory() function deletes an existing empty directory. If the directory is not empty, function fails with a return value zero. But most of the times, we call a function for removing a directory, what we want is to delete the directory structure completely including all files and sub-folders in it.

If you want this, there's DeleteDirectory() function to achieve it.

Source Code

BOOL DeleteDirectory(const TCHAR* sPath) {
    HANDLE hFind;  // file handle
    WIN32_FIND_DATA FindFileData;

    TCHAR DirPath[MAX_PATH];
    TCHAR FileName[MAX_PATH];

    _tcscpy(DirPath,sPath);
    _tcscat(DirPath,"\\*");    // searching all files
    _tcscpy(FileName,sPath);
    _tcscat(FileName,"\\");

    hFind = FindFirstFile(DirPath,&FindFileData); // find the first file
    if(hFind == INVALID_HANDLE_VALUE) return FALSE;
    _tcscpy(DirPath,FileName);
        
    bool bSearch = true;
    while(bSearch) { // until we finds an entry
        if(FindNextFile(hFind,&FindFileData)) {
            if(IsDots(FindFileData.cFileName)) continue;
            _tcscat(FileName,FindFileData.cFileName);
            if((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {

                // we have found a directory, recurse
                if(!DeleteDirectory(FileName)) { 
                    FindClose(hFind); 
                    return FALSE; // directory couldn't be deleted
                }
                RemoveDirectory(FileName); // remove the empty directory
                _tcscpy(FileName,DirPath);
            }
            else {
                if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
                    _chmod(FileName, _S_IWRITE); // change read-only file mode
                if(!DeleteFile(FileName)) {  // delete the file
                    FindClose(hFind); 
                    return FALSE; 
                }                 
                _tcscpy(FileName,DirPath);
            }
        }
        else {
            if(GetLastError() == ERROR_NO_MORE_FILES) // no more files there
            bSearch = false;
            else {
                // some error occured, close the handle and return FALSE
                FindClose(hFind); 
                return FALSE;
            }

        }

    }
    FindClose(hFind);  // closing file handle
 
    return RemoveDirectory(sPath); // remove the empty directory

}

DeleteDirectory() function uses a small companion IsDot() for checking '.' and '..' directory entries.

BOOL IsDots(const TCHAR* str) {
    if(_tcscmp(str,".") && _tcscmp(str,"..")) return FALSE;
    return TRUE;
}

Explanation

DeleteDirectory() is a recursive function which navigates through a directory structure using FindFirstFile() and FindNextFile() APIs. If it finds a file, it deletes it. On the other hand, if it finds a directory entry, it just calls itself to recursively delete the directory. It returns TRUE on success and FALSE on failure.

That's all there's to it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Web Developer
Pakistan Pakistan
Feroz Zahid has been programming in C/C++ for more than five years now. His experience includes Visual Basic, MFC, ATL, Managed C++ and Client/Server development. He has a strong taste of server side programming using PHP.

He is based in Karachi, Pakistan and works as a freelance programmer.

Feroz Zahid can be reached at ferozzahid [_at_] usa [_dot_] com.



Comments and Discussions

 
GeneralMy vote of 4 Pin
Jason Goepel4-Nov-11 14:05
Jason Goepel4-Nov-11 14:05 
Generala big 'WOW' Pin
Mohammad Elsheimy31-Mar-10 11:20
Mohammad Elsheimy31-Mar-10 11:20 
GeneralRe: a big 'WOW' Pin
Feroz Zahid31-Mar-10 21:38
Feroz Zahid31-Mar-10 21:38 
GeneralSimple way to delete read only files. Pin
anand choubey24-Feb-09 7:43
anand choubey24-Feb-09 7:43 
GeneralRe: Simple way to delete read only files. Pin
Feroz Zahid27-Feb-09 1:31
Feroz Zahid27-Feb-09 1:31 
GeneralDeleteDirectory() misses the first file. Pin
Neville Franks3-Dec-07 17:51
Neville Franks3-Dec-07 17:51 
GeneralDeleting Empty folders. Pin
DattaK23-May-07 8:19
DattaK23-May-07 8:19 
GeneralSimplified code (with bug-fix) Pin
naragana21-Jul-06 12:02
naragana21-Jul-06 12:02 
GeneralThanks Pin
FaxedHead3-Mar-06 18:49
FaxedHead3-Mar-06 18:49 
GeneralDo not use on WinCE Pin
hgode26-Sep-05 22:39
hgode26-Sep-05 22:39 
GeneralRe: Do not use on WinCE Pin
Feroz Zahid27-Sep-05 2:18
Feroz Zahid27-Sep-05 2:18 
GeneralRe: Do not use on WinCE Pin
paslecode6-Nov-09 2:55
paslecode6-Nov-09 2:55 
GeneralRe: Do not use on WinCE Pin
Feroz Zahid6-Nov-09 9:15
Feroz Zahid6-Nov-09 9:15 
GeneralRe: Do not use on WinCE Pin
Member 46770388-Feb-10 3:27
Member 46770388-Feb-10 3:27 
GeneralRe: Do not use on WinCE Pin
Feroz Zahid8-Feb-10 23:28
Feroz Zahid8-Feb-10 23:28 
GeneralA Nice Little Reference... Thanks Pin
Michael Bergman26-Jan-05 13:01
Michael Bergman26-Jan-05 13:01 
GeneralRe: A Nice Little Reference... Thanks Pin
Feroz Zahid27-Jan-05 3:17
Feroz Zahid27-Jan-05 3:17 
Generalsuggestion Pin
Todd Smith18-Dec-04 18:35
Todd Smith18-Dec-04 18:35 
GeneralRe: suggestion Pin
Feroz Zahid19-Dec-04 3:21
Feroz Zahid19-Dec-04 3:21 
GeneralRead Only Files Pin
Kacee Giger17-Dec-04 13:40
Kacee Giger17-Dec-04 13:40 
GeneralRe: Read Only Files Pin
Feroz Zahid17-Dec-04 22:27
Feroz Zahid17-Dec-04 22:27 
GeneralRe: Read Only Files Pin
Feroz Zahid17-Dec-04 22:39
Feroz Zahid17-Dec-04 22:39 
GeneralWhy reinvent the wheel. Pin
Prakash Nadar17-Dec-04 3:36
Prakash Nadar17-Dec-04 3:36 
GeneralRe: Why reinvent the wheel. Pin
ThatsAlok19-Dec-04 19:09
ThatsAlok19-Dec-04 19:09 
GeneralRe: Why reinvent the wheel. Pin
Prakash Nadar20-Dec-04 0:33
Prakash Nadar20-Dec-04 0:33 

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.