Folder Utility: Create Path, Remove Folder, Remove All Files
# ITC/# Tips of How to 2006. 11. 13. 23:25Folder 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.
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.
[CODE]
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);
}
[/CODE]
The DeleteAllFiles function deletes all the files (not folders) present in the specified path:
[CODE]
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);
}[/CODE]
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:
[CODE]
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);
}
[/CODE]
base on http://www.codeproject.com
By Birender Singh.
CreateDir function creates folders and subfolders thereby completing the whole path. This function overcomes the limitations of CreateDirectory Win32 API.
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.
[CODE]
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);
}
[/CODE]
The DeleteAllFiles function deletes all the files (not folders) present in the specified path:
[CODE]
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);
}[/CODE]
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:
[CODE]
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);
}
[/CODE]
base on http://www.codeproject.com