• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
VC飞虫
博客园    首页    新随笔    联系   管理    订阅  订阅
创建多级文件夹的方法

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("路径为找到!");

 

posted on 2011-11-14 22:53  VC飞虫  阅读(921)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3