递归创建、删除目录的几个函数
递归创建目录1:
需要#include <shlwapi.h>,并且配置好shlwapi.ib
BOOL CreateDirTree( LPCTSTR lpPath )
{
if( NULL == lpPath || _tcslen(lpPath)==0 )
{
return FALSE;
}
if( ::PathFileExists( lpPath) || ::PathIsRoot(lpPath) )
return TRUE;
TCHAR szParentpath[MAX_PATH] = _T("");
::lstrcpy( szParentpath, lpPath );
::PathRemoveBackslash( szParentpath );//去除路径最后的反斜杠
::PathRemoveFileSpec( szParentpath );//将路径末尾的文件名或文件夹和反斜杠去掉
if(0 == _tcscmp(lpPath, szParentpath))
return FALSE;
assert(0 != _tcscmp(lpPath, szParentpath));
if( CreateDirTree( szParentpath) )//递归创建直到上一层存在或是根目录
{
return ::CreateDirectory(lpPath, NULL);
}
else
{
return FALSE;
}
return TRUE;
}
递归创建目录2:
void __fastcall RecursiveDirectory(CString cstrDir) // 递归创建目录
{
if (cstrDir.GetLength() <= 3)//是根目录,无需创建目录
{
return;
}
if (cstrDir[cstrDir.GetLength()-1] == '\\') // 将路径改为目录
{
cstrDir.Delete(cstrDir.GetLength()-1, 1);
}
// 修改文件属性
WIN32_FIND_DATA wfd;
HANDLE hFind = FindFirstFile(cstrDir, &wfd); // 查找
if (hFind != INVALID_HANDLE_VALUE)
{
FindClose(hFind);
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
return;
}
// 创建当前目录的地目录失败
if (CreateDirectory(cstrDir,NULL) == false)
{// 退到上一级目录
CString wstrNewDir = cstrDir;
int n = wstrNewDir.ReverseFind('\\');
wstrNewDir = cstrDir.Left(n);
// 递归进入
RecursiveDirectory(wstrNewDir); // 递归本函数,再创建目录
// 递归退出后创建之前失败的目录
CreateDirectory(cstrDir,NULL); // 递归返回,在存在的目录上再建目录
}// 多级目录创建成功
}
递归创建目录3:
BOOL RecursiveDirectory(wstring wstrDir)
{
if (wstrDir.length() <= 3)
{
return FALSE;
}
if (wstrDir[wstrDir.length() - 1] == '\\')
{
wstrDir.erase(wstrDir.end() - 1);
}
if (PathFileExists(wstrDir.c_str()))
return TRUE;
if (CreateDirectory(wstrDir.c_str(), NULL) == false)
{
wstring wstrNewDir = wstrDir;
while (wstrNewDir[wstrNewDir.length() - 1] != '\\')
{
wstrNewDir.erase(wstrNewDir.length() - 1);
}
// delete '\\'
wstrNewDir.erase(wstrNewDir.length() - 1);
RecursiveDirectory(wstrNewDir);
CreateDirectory(wstrDir.c_str(), NULL);
}
if (!PathFileExists(wstrDir.c_str()))
return FALSE;
return TRUE;
}
递归创建目录4:
bool createDirectory(const char* pathName)
{
char path[MAX_PATH];
memset(path, 0x00, MAX_PATH);
const char* pos = pathName;
while ((pos = strchr(pos, '\\')) != NULL)
{
memcpy(path, pathName, pos - pathName + 1);
pos++;
if (_access(path, 0) == 0)
{
continue;
}
else
{
int ret = _mkdir(path);
if (ret == -1)
{
return false;
}
}
}
return true;
}
递归删除目录1:
system("rmdir /s /q dirname"); //dirname是要删除的目录名称,这种方式,在使用MFC程序的时候出闪过一个CMD的窗口
/s是级联删除 /q 是不提示(在命令行下操作的话,如果不加这个开关,会有提示确认是否删除目录,而在程序中不允许停下)
递归删除目录2:
SHFILEOPSTRUCT FileOp;
FileOp.fFlags = FOF_NOCONFIRMATION;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = ".\\tempDir";
FileOp.pTo = NULL;
FileOp.wFunc = FO_DELETE;
SHFileOperation(&FileOp);
此处有一个地方要留心一下,就是FileOp.pFrom这个参数,它使用的字符串一定是要'\0'结尾的,这个地方使用".\tempDir",这个字符串默认的结束字符就是'\0',所以如果存在这个目录或者文件的话,一定可以将其删除,如果像下面这样写的话就会出错:
std::string delPath = ".\tempDir";
FileOp.pFrom = delPath.c_str(); // 此时字符串没有以'\0'结尾,所以删除的时候会出错
递归删除目录3:
bool deleteDirectory( char* pathName)
{
struct _finddata_t fData;
memset(&fData, 0, sizeof(fData));
if (_chdir(pathName) != 0) //_chdir函数设置当前目录
{
printf("chdir failed: %s\n",pathName);
return false;
}
intptr_t hFile = _findfirst("*",&fData); //参数1:char *类型,"*"表示通配符,可以查找文件、文件夹
if(hFile == -1)
{
printf("_findfirst error!\n");
return false;
}
do
{
if(fData.name[0] == '.')
continue;
if(fData.attrib == _A_SUBDIR) //子文件夹
{
char dirPath[MAX_PATH];
memset(dirPath,0,sizeof(pathName));
strcpy_s(dirPath,pathName);
strcat_s(dirPath,"\\");
strcat_s(dirPath,fData.name);
deleteDirectory(dirPath); //recursion subdir
printf("remove dir: %s\n",dirPath);
_chdir("..");
_rmdir(dirPath);
}
else
{
char filePath[MAX_PATH];
memset(filePath,0,sizeof(filePath));
strcpy_s(filePath,pathName);
strcat_s(filePath,"\\");
strcat_s(filePath,fData.name);
remove(filePath);
printf("remove file: %s\n",filePath);
}
}while(_findnext(hFile,&fData) == 0);
_findclose(hFile); //close
return true;
}