需求如下:在一个根目录下先创建一组文件夹,再在该文件夹下创建子文件夹,再在子文件夹下面创建子文件夹以及一个txt 我的解决思路:把创建好的文件夹放进容器,在容器中遍历执行操作 界面如下 代码如下
void CFolderCreatorDlg::OnBnClickedCreate() { CString str1; CString str2; CString path; CString folderName; CString m_FilePath; CString m_SubFilePath; CString m_SubFilePath2; CString str3; CString str4; vector<CString>Folder;//ME\sub101 vector<CString>SubFolder;//sub01 // vector<CString>SubFolder2;//ME\sub101\sub03 - ME_01 CFile file; CString txtPath; string smat; CString str5; GetDlgItem(IDC_EDIT2)->GetWindowText(str1); GetDlgItem(IDC_EDIT3)->GetWindowText(str2); GetDlgItem(IDC_EDIT1)->GetWindowText(path); int numStart= _ttoi(str1);//CString->int int numNum = _ttoi(str2);//CString->int /*第一轮创建*/ for (int i = numStart; i < numStart + numNum; i++) { folderName.Format(_T("%d"), i);//int->CString if (folderName.GetLength() == 1)//如果是1,2,3,.....9 { folderName = _T("0") + folderName;//变成01,02,03,。。。09 } str4= _T("sub") + folderName;//sub01,sub02, SubFolder.push_back(str4);//把sub01.....存入SubFolder m_FilePath = path + "\\" + str4;//path +sub01,path +sub02 Folder.push_back(m_FilePath);//把path +sub01,path +sub02存入Folder if (FALSE == CreateDirectory(m_FilePath, NULL))//没有该文件夹就创建文件夹 { if (ERROR_ALREADY_EXISTS == GetLastError()) { TRACE("\n this path is invalid"); } else { TRACE("\n this path is exists"); } } } /*第二轮创建*/ for (int j = 0; j < numNum; j++) { m_SubFilePath = Folder[j];//在sub01.......下面操作 for (int k = 0; k < 5; k++)//每个sub下创建5个文件夹 { str3.Format(_T("%d"), k + 1);//int->CString if (str3.GetLength() == 1)//如果是1,2,3,4,5 { str3 = _T("0") + str3;//变成01,02,03,04,05 } m_SubFilePath2 = m_SubFilePath + "\\" + SubFolder[j] + "-ME_" + str3;//path +"\\"+sub01+"\\"+sub01+"-ME_"+01 SubFolder2.push_back(m_SubFilePath2);//把path +"\\"+sub01+"\\"+sub01+"-ME_"+01存入容器 if (FALSE == CreateDirectory(m_SubFilePath2, NULL))//在sub01....下面创建文件夹 { if (ERROR_ALREADY_EXISTS == GetLastError()) { TRACE("\n this path is invalid"); } else { TRACE("\n this path is exists"); } } } } /*第三轮创建*/ for (int p = 0; p < SubFolder2.size(); p++) { m_SubFilePath = SubFolder2[p];//path +"\\"+sub01+"\\"+sub01+"-ME_"+01 //MessageBox(m_SubFilePath); smat= CT2A(m_SubFilePath.GetBuffer(0));//CString->string // str5.Format(_T("%d"),p); str5 = m_SubFilePath.Mid(m_SubFilePath.GetLength() - 2, 2);//截取m_SubFilePath的后2位,01,02,03,04,05 int q = smat.find("-");//返回“-”的位置 int w = smat.rfind("\\");//返回“\”最后一次出现的位置 //m_SubFilePath2 = m_SubFilePath + "\\" + m_SubFilePath.Mid(w-3, q-w+3) + _T("_")+ str5+_T("_Pic"); m_SubFilePath2 = m_SubFilePath + "\\" + m_SubFilePath.Mid(w - 3, q - w + 3) + str5 + _T("_Pic");path +"\\"+sub01+"\\"+sub01+"-ME_"+01+"\\"+sub01-ME_01_Pic //MessageBox(m_SubFilePath2); if (FALSE == CreateDirectory(m_SubFilePath2, NULL))//在sub01-ME_01下面创建文件夹 { if (ERROR_ALREADY_EXISTS == GetLastError()) { TRACE("\n this path is invalid"); } else { TRACE("\n this path is exists"); } } //txtPath = m_SubFilePath + "\\" + m_SubFilePath.Mid(w - 3, q - w + 3) + _T("_") + str5 + _T("_time.txt"); txtPath = m_SubFilePath + "\\" + m_SubFilePath.Mid(w - 3, q - w + 3) + str5 + _T("_time.txt"); file.Open(txtPath, CFile::modeCreate);//创建txt file.Close(); } MessageBox(_T("文件夹生成完毕")); }结果展示