- void CCreatSDLDlg::FindAllFile(CString Path)
- {
- CFileFind finder;
- CString directorpath;//表示当前文件夹(所有文件夹包括子文件夹)的全路径
- CString filerootpath;//表示当前文件夹(所有文件夹包括子文件夹)在总文件夹(即m_INIPATH)下的路径
- BOOL working = finder.FindFile(Path+"\\\\"+"*.*");
- while (working)
- {
- working = finder.FindNextFile();
- if (finder.IsDots())
- continue;
- if (finder.IsDirectory())
- {
- directorpath=finder.GetFilePath();//获取当前文件夹全路径
- filerootpath=directorpath;
- filerootpath.Replace(m_INIPATH,"xxx");//注:trim left不能正常使用
- filerootpath.TrimLeft("xxx");//这三句就是获取子路径
- CreateDirectory(m_Path+filerootpath,NULL);//将获取到的子路径与目标文件夹路径结合生产新的对应文件夹
- FindAllFile(directorpath);//将当前文件夹全路径传入递归进行新一轮的搜索
- }
- else
- {
- directorpath=finder.GetFilePath();//获取当前文件(不是文件夹)的全路径
- filerootpath=directorpath;
- filerootpath.Replace(m_INIPATH,"xxx");
- filerootpath.TrimLeft("xxx");//获取当前文件的子路径
- filerootpath.Replace("原文件名里包含的STR","新的STR");//修改文件名
- //比如原来文件叫file1,file2,file3 filerootpath.Replace("file","myfilename")即可
- myfilename1,myfilename2,myfilename3
- CStdioFile file_r;
- if ( file_r.Open(directorpath, CFile::modeRead ) )//打开当前文件
- {
- CStdioFile file_w;
- if ( file_w.Open(m_Path+filerootpath,CFile::modeWrite|CFile::modeCreate))//在对应处生成新文件
- {
- CString strLine;
- while(file_r.ReadString(strLine))
- {
- strLine.Replace("data","mydata");//逐行修改文件所有内容
- strLine += "\\n";
- file_w.WriteString(strLine);
- }
- file_w.Close();
- }
- file_r.Close();
- }
- else
- {
- AfxMessageBox("打开文件失败"+file_r.GetFileName());
- }
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/291020136749.html
来源: http://www.codesnippet.cn/detail/291020136749.html