Click here to Skip to main content
13,255,340 members (49,778 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 6 Jun 2005

Folder Utility: Create Path, Remove Folder, Remove All Files

, 6 Jun 2005
Rate this:
Please Sign up or sign in to vote.
CreateDir function creates folders and subfolders thereby completing the whole path. This function overcomes the limitations of CreateDirectory Win32 API.


The CreateDir function creates folders and subfolders thereby completing the whole path passed to it. If the folder already exists, it is left unaffected, but if it doesn't exist, it is created. The CreateDirectory WIN32 API lets us create a directory, but it works only if the parent directory already exists. This function overcomes this limitation.

void CreateDir(char* Path)
 char DirName[256];
 char* p = Path;
 char* q = DirName; 
   if (('\\' == *p) || ('/' == *p))
     if (':' != *(p-1))
        CreateDirectory(DirName, NULL);
   *q++ = *p++;
   *q = '\0';
 CreateDirectory(DirName, NULL);

The DeleteAllFiles function deletes all the files (not folders) present in the specified path:

void DeleteAllFiles(char* folderPath)
 char fileFound[256];
 WIN32_FIND_DATA info;
 HANDLE hp; 
 sprintf(fileFound, "%s\\*.*", folderPath);
 hp = FindFirstFile(fileFound, &info);
       sprintf(fileFound,"%s\\%s", folderPath, info.cFileName);
    }while(FindNextFile(hp, &info)); 

The EmptyDirectory function deletes all the contents from a specified directory. The RemoveDirectory WIN32 API deletes an existing empty directory, but it doesn't work if the directory isn't empty. This function overcomes this limitation:

void EmptyDirectory(char* folderPath)
 char fileFound[256];
 WIN32_FIND_DATA info;
 HANDLE hp; 
 sprintf(fileFound, "%s\\*.*", folderPath);
 hp = FindFirstFile(fileFound, &info);
        if (!((strcmp(info.cFileName, ".")==0)||
              (strcmp(info.cFileName, "..")==0)))
          if((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==
              string subFolder = folderPath;
              sprintf(fileFound,"%s\\%s", folderPath, info.cFileName);
              BOOL retVal = DeleteFile(fileFound);
    }while(FindNextFile(hp, &info)); 


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

Birender Singh
Technical Lead Leading Services Organization
India India
Working as a Technology Lead in Mobility domain (Mobile applications: Android, IPhone) with an India Based Leading Services Organization.

Areas of interests includes SOA, Mobility, Design Patterns.

As part of services organization, worked with market Leader Firms into Networking, Security & Mobility spaces.

You may also be interested in...


Comments and Discussions

Generaluseful Pin
Jeff cat28-Oct-14 16:46
memberJeff cat28-Oct-14 16:46 
GeneralCheck for FILE_ATTRIBUTE_READONLY attribute before try to delete file [modified] Pin
Julian Popov2-Feb-10 4:22
memberJulian Popov2-Feb-10 4:22 
GeneralA permission request. Pin
Vincent-Lin31-Aug-09 21:14
memberVincent-Lin31-Aug-09 21:14 
GeneralNo success/fail returned Pin
Neville Franks3-Dec-07 18:46
memberNeville Franks3-Dec-07 18:46 
GeneralUse the UNICODE versions.... Pin
Ben Burnett14-Sep-07 10:20
memberBen Burnett14-Sep-07 10:20 
GeneralThank you Pin
YoSilver9-Jan-07 8:35
memberYoSilver9-Jan-07 8:35 
Generaleasier way Pin
David White11-Apr-06 6:50
memberDavid White11-Apr-06 6:50 
GeneralRe: easier way Pin
Michael Stammberger8-May-09 0:39
memberMichael Stammberger8-May-09 0:39 
Generali got help from your code Pin
ewighell6-Apr-06 23:06
memberewighell6-Apr-06 23:06 

Thank you very much!!!

I am learning C++ and English
GeneralGood, but... Pin
Geert van Horrik6-Jun-05 20:59
memberGeert van Horrik6-Jun-05 20:59 
QuestionMAX_PATH ? Pin
Krodex6-Jun-05 9:23
sussKrodex6-Jun-05 9:23 
GeneralRead Only Files Pin
Kacee Giger6-Jun-05 7:05
memberKacee Giger6-Jun-05 7:05 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 6 Jun 2005
Article Copyright 2005 by Birender Singh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid