C++递归创建文件夹

 根据传入的参数递归进行目录的创建。

 

函数描述:

  递归创建目录。 

入参:

  所要创建的目录。

返回值:

  创建成功,返回TRUE;否则返回FALSE。

 

 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 }   

 

posted on 2020-04-11 19:44  Arthurian  阅读(2186)  评论(0编辑  收藏  举报