Click here to Skip to main content
Click here to Skip to main content

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

By , 6 Jun 2005
 

Introduction

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; 
 while(*p)
 {
   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);
 do
    {
       sprintf(fileFound,"%s\\%s", folderPath, info.cFileName);
       DeleteFile(fileFound);
 
    }while(FindNextFile(hp, &info)); 
 FindClose(hp);
}

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);
 do
    {
        if (!((strcmp(info.cFileName, ".")==0)||
              (strcmp(info.cFileName, "..")==0)))
        {
          if((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==
                                     FILE_ATTRIBUTE_DIRECTORY)
          {
              string subFolder = folderPath;
              subFolder.append("\\");
              subFolder.append(info.cFileName);
              EmptyDirectory((char*)subFolder.c_str());
              RemoveDirectory(subFolder.c_str());
          }
          else
          {
              sprintf(fileFound,"%s\\%s", folderPath, info.cFileName);
              BOOL retVal = DeleteFile(fileFound);
          }
        }
 
    }while(FindNextFile(hp, &info)); 
 FindClose(hp);
}

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

Birender Singh
Technical Lead Leading Services Organization
India India
Member
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.

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralCheck for FILE_ATTRIBUTE_READONLY attribute before try to delete file [modified]memberJulian Popov2 Feb '10 - 3:22 
GeneralA permission request.memberVincent-Lin31 Aug '09 - 20:14 
GeneralNo success/fail returnedmemberNeville Franks3 Dec '07 - 17:46 
GeneralUse the UNICODE versions....memberBen Burnett14 Sep '07 - 9:20 
GeneralThank youmemberYoSilver9 Jan '07 - 7:35 
Generaleasier waymemberDavid White11 Apr '06 - 5:50 
GeneralRe: easier waymemberMichael Stammberger7 May '09 - 23:39 
Generali got help from your codememberewighell6 Apr '06 - 22:06 
v
 
Thank you very much!!!
 
-------------------
I am learning C++ and English
GeneralGood, but...memberGeert van Horrik6 Jun '05 - 19:59 
QuestionMAX_PATH ?sussKrodex6 Jun '05 - 8:23 
GeneralRead Only FilesmemberKacee Giger6 Jun '05 - 6:05 

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

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