C++ 刪除指定路徑資料夾
阿新 • • 發佈:2019-01-23
/************************************************************************/
/* 刪除指定路徑下的資料夾
/* DirName: 檔案路徑
/* bNeedDel: 是否刪除資料夾
/************************************************************************/
void DeleteDirectory(char *DirName, bool bNeedDel)
{
CFileFind tempFind;
char tempFileFind[MAX_PATH];
sprintf(tempFileFind,"%s\\*.*",DirName);
BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind); //判斷資料夾下是否存在檔案
while(IsFinded) //存在
{
IsFinded=(BOOL)tempFind.FindNextFile(); //判斷是否存在下一個檔案
if(!tempFind.IsDots())
{
char foundFileName[MAX_PATH] = {0};
strcpy(foundFileName,tempFind.GetFileName().GetBuffer(MAX_PATH));
if(tempFind.IsDirectory())
{
char tempDir[MAX_PATH] = {0};
sprintf(tempDir,"%s\\%s",DirName,foundFileName);
DeleteDirectory(tempDir, bNeedDel);
}
else
{
char tempFileName[MAX_PATH] = {0};
sprintf(tempFileName,"%s\\%s",DirName,foundFileName);
DeleteFile(tempFileName);
}
}
}
tempFind.Close();
if (bNeedDel)
{
RemoveDirectory(DirName);
}
}
/* 刪除指定路徑下的資料夾
/* DirName: 檔案路徑
/* bNeedDel: 是否刪除資料夾
/************************************************************************/
void DeleteDirectory(char *DirName, bool bNeedDel)
{
CFileFind tempFind;
char tempFileFind[MAX_PATH];
sprintf(tempFileFind,"%s\\*.*",DirName);
BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind); //判斷資料夾下是否存在檔案
while(IsFinded) //存在
{
IsFinded=(BOOL)tempFind.FindNextFile(); //判斷是否存在下一個檔案
if(!tempFind.IsDots())
{
char foundFileName[MAX_PATH] = {0};
strcpy(foundFileName,tempFind.GetFileName().GetBuffer(MAX_PATH));
if(tempFind.IsDirectory())
{
char tempDir[MAX_PATH] = {0};
sprintf(tempDir,"%s\\%s",DirName,foundFileName);
DeleteDirectory(tempDir, bNeedDel);
}
else
{
char tempFileName[MAX_PATH] = {0};
sprintf(tempFileName,"%s\\%s",DirName,foundFileName);
DeleteFile(tempFileName);
}
}
}
tempFind.Close();
if (bNeedDel)
{
RemoveDirectory(DirName);
}
}