创建多层目录
1 BOOL CTestToolCtr::CreateFolder(CString strNewFolder) 2 { 3 /********************************************************************** 4 [Name ] 5 CreateFolder 6 7 [Function ] 8 创建文件夹 9 10 [Param ] 11 CString strNewFolder 创建的目录 12 13 [Return ] 14 TRUE: 创建成功 15 FALSE:创建失败 16 17 [Remarks ] 18 ***********************************************************************/ 19 if (PathFileExists(strNewFolder)) 20 { 21 return FALSE; 22 } 23 else 24 { 25 return MakeDir((LPCTSTR)strNewFolder); 26 } 27 return TRUE; 28 } 29 30 BOOL CTestToolCtr::MakeDir(const char* szPath) 31 { 32 char copy_path[512]; 33 memset(copy_path, 0, sizeof(copy_path)); 34 35 int len = 0; 36 char *last_mozi = NULL; 37 char *mozi = NULL; 38 char d_buf[512], dd_buf[512]; 39 memset(d_buf, 0, sizeof(d_buf)); 40 memset(dd_buf, 0, sizeof(dd_buf)); 41 42 sprintf_s(copy_path, sizeof(copy_path), szPath); 43 len = strlen(copy_path); 44 last_mozi = CharPrev(copy_path, ©_path[len]); 45 46 if (last_mozi == (©_path[len-1])) 47 { 48 if (copy_path[len-1] == '\\' || copy_path[len-1] == '/') 49 { 50 copy_path[len-1] = 0x00; 51 } 52 } 53 54 CharPrev(copy_path, copy_path); 55 mozi = CharNext(copy_path); 56 while (*mozi != '\0') 57 { 58 if (*mozi == '\\' || *mozi == '/') 59 { 60 memset(d_buf, 0, sizeof(d_buf)); 61 memcpy(d_buf, copy_path, mozi-copy_path); 62 sprintf_s(dd_buf, sizeof(d_buf), "%s", d_buf); 63 if (!PathFileExists(dd_buf)) 64 { 65 if (!CreateDirectory(dd_buf, NULL)) 66 { 67 return FALSE; 68 } 69 } 70 } 71 mozi = CharNext(copy_path); 72 } 73 sprintf_s(dd_buf,sizeof(copy_path), "%s", copy_path); 74 75 if (!PathFileExists(dd_buf)) 76 { 77 if (!CreateDirectory(dd_buf,NULL)) 78 { 79 return FALSE; 80 } 81 } 82 return TRUE; 83 }