Click here to Skip to main content
6,293,171 members and growing! (11,638 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » General     Intermediate

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

By Birender Singh

CreateDir function creates folders and subfolders thereby completing the whole path. This function overcomes the limitations of CreateDirectory Win32 API.
C, VC6, Windows, Visual Studio, MFC, Dev
Posted:6 Jun 2005
Views:46,303
Bookmarked:21 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
22 votes for this article.
Popularity: 5.29 Rating: 3.94 out of 5
3 votes, 13.6%
1
1 vote, 4.5%
2
1 vote, 4.5%
3
3 votes, 13.6%
4
14 votes, 63.6%
5

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


Member

Occupation: Web Developer
Location: United Kingdom United Kingdom

Other popular Files and Folders articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
GeneralNo success/fail returned PinmemberNeville Franks18:46 3 Dec '07  
GeneralUse the UNICODE versions.... PinmemberBen Burnett10:20 14 Sep '07  
GeneralThank you PinmemberYoSilver8:35 9 Jan '07  
Generaleasier way PinmemberDavid White6:50 11 Apr '06  
GeneralRe: easier way PinmemberMichael Stammberger0:39 8 May '09  
Generali got help from your code Pinmemberewighell23:06 6 Apr '06  
GeneralGood, but... PinmemberGeert van Horrik20:59 6 Jun '05  
GeneralMAX_PATH ? PinsussKrodex9:23 6 Jun '05  
GeneralRead Only Files PinmemberKacee Giger7:05 6 Jun '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Jun 2005
Editor: Rinish Biju
Copyright 2005 by Birender Singh
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project