C ++ create folder recursively

 Recursively create the directory based on the parameters passed in.

 

Function description:

  Create directories recursively. 

Entry:

  The directory to be created.

return value:

  If the creation is successful, return TRUE; otherwise, return 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); // Remove the backslash 
at the      end of the path 16 if (NULL == pTmp)
 17      {
 18          return FALSE;
 19      }
 20  
21      BOOL bRet = PathRemoveFileSpec (szParentpath); // The end of the path File name or folder and backslashes are removed 
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)) // Recursively created until the previous layer exists or the root directory 
33      {
 34          return CreateDirectory (lpPath, NULL);
 35      }
 36      else 
37      {
 38          return FALSE;
 39      }
 40  
41      return TRUE;
 42 }

 

 

Author:耑new New, released in   blog Park

Please indicate the source for the reprint, welcome to communicate by email: [email protected]

Guess you like

Origin www.cnblogs.com/Arthurian/p/12681789.html