1.利用CreateDirectory()
步骤:先检查Directory是否存在,不存在则循环一级一级创建,直到没有文件夹需要创建
1 DirExist(const TCHAR *pszDirName)
2 {
3 WIN32_FIND_DATA fileinfo;
4 TCHAR _szDir[_MAX_PATH];
5 _tcscpy(_szDir,pszDirName);
6 int nLen = _tcsclen(_szDir);
7 if((_szDir[nLen-1] == '//') || (_szDir[nLen-1] == '/'))
8 {
9 _szDir[nLen-1] = '/0';
10 }
11 HANDLE hFind = ::FindFirstFile(_szDir,&fileinfo);
12 if(hFind == INVALID_HANDLE_VALUE)
13 {
14 return false;
15 }
16 if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
17 {
18 ::FindClose(hFind);
19 return true;
20 }
21 ::FindClose(hFind);
22 return false;
23 }
24
25 FindDir(CString strDir)
26 {
27 int nStart = 0;
28 nStart = strDir.Find('\\',nStart);
29 while(nStart!=-1||nStart>strDir.GetLength()){
30 nStart++;
31 nStart = strDir.Find('\\',nStart);
32 if(nStart==-1)
33 break;
34 CString strTempDir=strDir.Left(nStart);
35 CFile f;
36 BOOL bExist = DirExist(strTempDir.GetBuffer(0));
37 if(!bExist)
38 CreateDirectory(strTempDir,NULL);
39 /* if(bExist)
40 TRACE("Directory %s is Exist!\n",strTempDir);
41 else{
42 TRACE("Directory %s is not Exist!\n",strTempDir);
43 CreateDirectory(strTempDir,NULL);
44 if(DirExist(strTempDir.GetBuffer(0)))
45 {
46 // TRACE(" ---> Create %s Success!\n",strTempDir);
47 }else{
48 TRACE(" ---> Create %s Fail!\n",strTempDir);
49 }
50 }*/
51 }
52 return TRUE;
53 }
其中 FindDir为创建函数,DirExist用于检测文件夹的存在。
2.利用CMD的SHELL命令mkdir
可以使用 system("MKDIR d:\\a\\b\\c");
但会出现一个一闪即逝的黑框--》CMD窗口。
也可以使用 WinExec(); 其中SW_HIDE可以避免CMD窗口的闪现。
UINT uError = WinExec("cmd /c mkdir d:\\a\\b\\c",SW_HIDE);
if(uError == ERROR_BAD_FORMAT)
AfxMessageBox("格式错误!");
if(uError == ERROR_FILE_NOT_FOUND)
AfxMessageBox("文件未找到!");
if(uError == ERROR_PATH_NOT_FOUND)
AfxMessageBox("路径为找到!");