C++递归创建文件夹
2021-02-20 15:18
                         标签:else   count   home   文件夹   line   ||   har   tde   uri     根据传入的参数递归进行目录的创建。   函数描述:   递归创建目录。  入参:   所要创建的目录。 返回值:   创建成功,返回TRUE;否则返回FALSE。       作者:耑新新,发布于  博客园 转载请注明出处,欢迎邮件交流:zhuanxinxin@aliyun.com C++递归创建文件夹 标签:else   count   home   文件夹   line   ||   har   tde   uri    原文地址:https://www.cnblogs.com/Arthurian/p/12681789.html
 1 BOOL CreateDirTree(LPCTSTR lpPath)
 2 {
 3     if( (NULL == lpPath) || (0 == _tcslen(lpPath)))
 4     {
 5         return FALSE;
 6     }
 7 
 8     if((TRUE == PathFileExists(lpPath)) || (TRUE == PathIsRoot(lpPath)) )
 9     {
10         return TRUE;
11     }
12     TCHAR szParentpath[MAX_PATH] = _T("");
13     _tcscpy_s( szParentpath, _countof(szParentpath), lpPath);
14 
15     TCHAR *pTmp = PathRemoveBackslash(szParentpath );//去除路径最后的反斜杠
16     if (NULL == pTmp)
17     {
18         return FALSE;
19     }
20 
21     BOOL bRet = PathRemoveFileSpec(szParentpath );//将路径末尾的文件名或文件夹和反斜杠去掉
22     if (FALSE == bRet)
23     {
24         MyOutputDebugMsg(_T("%s %d PathRemoveFileSpec Failed"), __TFILE__, __LINE__);
25     }
26 
27     if(0 == _tcscmp(lpPath, szParentpath))
28     {
29         return FALSE;
30     }
31 
32     if(CreateDirTree(szParentpath))//递归创建直到上一层存在或是根目录
33     {
34         return CreateDirectory(lpPath, NULL);
35     }
36     else
37     {
38         return FALSE;
39     }
40 
41     return TRUE;
42 }
上一篇:C语言程序设计实验报告(4)