MFC根据输入路径创建文件夹及文件夹下的默认文件夹

来源:互联网 发布:淘宝哪个节日打折最狠 编辑:程序博客网 时间:2024/06/07 01:09

函数名:CreateDir

函数功能:根据传入的路径创建文件夹及文件夹下的默认文件夹

参数:strSetPath  strDefaultDir

(入口)strSetPath:传入的完整路径

(入口)strDefaultDir:在上述传入的路径下的默认文件夹

返回值:无

额外说明:该函数会始终在传入路径的参数下额外的创建一个默认文件夹

作者:weekdawn


void CMD5TestDlg::CreateDir(CString strSetPath, CString strDefaultDir)//suppose as : strSetPath = D:\test\temp\demo{//默认生成的文件夹if (strSetPath.IsEmpty()){CString listStr = strDefaultDir;CString dirPath;dirPath = _T("./") + listStr;if (!PathFileExists(dirPath)){CreateDirectory(dirPath,NULL);}}//如果指定了目录,则根据目录生成文件夹else{//将路径根据"\"分割开CStringArray editArr;int nPos = strSetPath.Find(_T("\\"));//如果找到路径分隔符,则根据路径创建文件夹if (nPos != -1){CString temp;temp = _T("");while(0 <= nPos){temp = strSetPath.Left(nPos);if(!temp.IsEmpty())editArr.Add(temp);strSetPath = strSetPath.Right(strSetPath.GetLength() - nPos -1);nPos = strSetPath.Find(_T("\\"));}if (!strSetPath.IsEmpty()){editArr.Add(strSetPath);}int nSize = editArr.GetSize();//创建文件夹CString dirPath;dirPath = editArr.GetAt(0) + _T("\\") + editArr.GetAt(1);//D:\testif (!PathFileExists(dirPath)){CreateDirectory(dirPath,NULL);}for (int i = 2; i < nSize; i++){dirPath = dirPath + _T("\\") + editArr.GetAt(i);if (!PathFileExists(dirPath)){CreateDirectory(dirPath,NULL);}}//创建指定目录下的默认目录CString listStr;listStr = strDefaultDir;dirPath = dirPath + _T("\\") + listStr;if (!PathFileExists(dirPath)){CreateDirectory(dirPath,NULL);}} //如果没有路径分隔符"\",则在当前目录下生成改文件夹//like : strSetPath = testelse{if (!PathFileExists(strSetPath)){CreateDirectory(strSetPath,NULL);}//创建指定目录下的默认目录CString listStr;CString dirPath;listStr = strDefaultDir;dirPath = strSetPath + _T("\\") + listStr;if (!PathFileExists(dirPath)){CreateDirectory(dirPath,NULL);}}}}