根据txt文件名,查找并导出文件夹中fpt路径。诺,一看就明白,VC6.0MFC基于Dialog的工程。 我恋旧,一直用vc6.

1.单击“选目录”按钮,选择要遍历的文件夹(可以包含子文件夹)。
2.单击“选路径”按钮,选择txt文档。
3.单击“确定”按钮,完成遍历操作,并把目录输出到新建的txt文件中。
反正好多代码我也没看,以前用过直接复制粘贴的稍微改了下。
主要代码:
1.选目录函数。
void CRenameFileDlg::OnButton1()
{
UpdateData();
char buf[MAX_PATH] = {0};
if (select_any(buf))
{
m_pathname=buf;
UpdateData(FALSE);
}
}
int select_any(char path[MAX_PATH], char *title = "选择目录 ...")
{
BROWSEINFO bi;
ITEMIDLIST *pidl;
bi.hwndOwner = NULL;
bi.pidlRoot = NULL;
bi.pszDisplayName = path;
bi.lpszTitle = title;
bi.ulFlags = BIF_EDITBOX;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;
pidl = SHBrowseForFolder(&bi);
return (pidl && SHGetPathFromIDList(pidl, path));
}
2.选路径按钮。
void CRenameFileDlg::OnButton2()
{
UpdateData();
//生成一个打开对话框
CFileDialog *lpszOpenFile;
lpszOpenFile = new CFileDialog
(TRUE,"ccd.txt","",OFN_FILEMUSTEXIST |OFN_HIDEREADONLY , "文件类型(*.txt)|*.txt|所有文件(*.*)|*.*|");
if(lpszOpenFile->DoModal() == IDOK )//假如点击对话框确定按钮
{
m_pathname2 = lpszOpenFile->GetPathName();//得到打开文件的路径
SetWindowText(m_pathname2);//在窗口标题上显示路径
}
UpdateData(FALSE);
}
3.遍历文件夹函数。
void CRenameFileDlg::h_FileFile(CString path)
{
CString filePath=path+_T("//*.*");//能遍历到子文件夹
//CString filePath=path+_T("//*.bmp"); 有后缀后不能遍历子文件夹
CFileFind fileFinder;
BOOL bFinished = fileFinder.FindFile(filePath);
while(bFinished) //每次循环对应一个类别目录
{
bFinished = fileFinder.FindNextFile();
if(fileFinder.IsDirectory() && !fileFinder.IsDots()) //若是目录则递归调用此方法
{
h_FileFile(fileFinder.GetFilePath());
}
else //判断文件名是否在txt中
{
//获取路径最后文件夹名字
CString path2=fileFinder.GetFilePath();
//获取文件类型
CString fileName = fileFinder.GetFileName();////获得不包含路径的 文件名.后缀
CString f_fix,f_fname;
f_fix=fileName.Right(4);
f_fname=fileName.Left(23);
if (f_fix==".fpt") //这里遍历文件夹和子文件夹的fpt后缀的文件
{
////////读取txt中的文件名/////////
CStdioFile file;
if(!file.Open(m_pathname2,CFile::modeRead))
{
MessageBox("can not open file!");
return;
}
CString strLine,strTemp,strrow; //strLine:txt每行的内容
while(file.ReadString(strLine))
{
CString str,p;
str= strLine.GetBufferSetLength(strLine.GetLength());
if(strLine!="")
{
if (f_fname==str.Left(23))//
{
strtemp=strtemp+fileFinder.GetFilePath()+"\r\n";
}
}
}
///////////////////////
}
}//end_else
}//end while
}
4.确定按钮函数。
void CRenameFileDlg::OnBUTTON2ok()
{
strtemp="";
UpdateData();
if (m_pathname=="")
{
MessageBox("请选择路径","提示",MB_OK);return;
}
h_FileFile(m_pathname);
theApp.AddtoTxt(strtemp);
AfxMessageBox(" 操作已完成!");
}
以上都是**Dlg.cpp的函数,还有两个函数封装在了**.cpp里,当全局函数了。不要问为什么,我懒死了,用以前的框架直接改的,懒得换地方。
5.字符流写入txt文件。
CString CRenameFileApp::GetExePath(void)
{
char szFilePath[MAX_PATH + 1]={0};
GetModuleFileNameA(NULL, szFilePath, MAX_PATH);//1111
(strrchr(szFilePath, '\\')+1)[0] = 0; // 删除文件名,只获得路径字串
CString path = szFilePath;
return path;
}
void CRenameFileApp::AddtoTxt(CString strtemp)
{
CString h_path;
h_path=GetExePath();
h_path= h_path+"fpt路径.txt";
CFile file;
if(!file.Open(_T(h_path),CFile::modeReadWrite| CFile::typeBinary))
{
file.Open(_T(h_path),CFile::modeCreate|CFile::modeReadWrite| CFile::typeBinary);
}
file.SeekToEnd();
file.Write(strtemp,strtemp.GetLength());
file.Close();
}
源代码。vc6工程的。
链接: http://pan.baidu.com/s/1gf4jdCZ 密码: dbxn
浙公网安备 33010602011771号