批处理—TXT文档中的E-MAIL 条数统计出来

来源:互联网 发布:统计学 从数据到结论 编辑:程序博客网 时间:2024/04/29 16:52

// 选择目录
void CFindEmailDlg::fu_SelectDir(void)
{
BROWSEINFO bi;
ITEMIDLIST *pidl;
char chPath[1024];
bi.hwndOwner = NULL;
bi.pidlRoot = NULL;
bi.pszDisplayName = chPath;
bi.lpszTitle = _T("选择一个目录");
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;

pidl = SHBrowseForFolder( &bi );          /* Display "Select Folde
                                             r" dialog box, Get the  
                                             folder name and co
                                             nvert it into a ITEMLIST   data structure. */


if ( pidl == NULL )
       chPath[0] = 0;

if (!SHGetPathFromIDList( pidl, chPath ))    /* Retrieve folder name from ITEMLIST structure. */
       chPath[0] = 0;
m_edtPath.SetWindowText(chPath);
//m_edtPath.SetWindowTextW(chPath);
}
//按钮“查找目录的代码”,调用查找目录函数
void CFindEmailDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
fu_SelectDir();
}

//按钮“开始统计”的代码,开始统计,文件中的邮件以制表符分隔"/t"
void CFindEmailDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
WIN32_FIND_DATA findData;
HANDLE hFind;
CString strPath;
CString strFileName;
m_edtPath.GetWindowText(strPath);
//如果路径名最后一个不是"/",则添加"/"
if(strPath.Mid(strPath.GetLength(),1) != "//")
   strPath+="//";
//return;
hFind = ::FindFirstFile(strPath+"*.*",&findData);
if ( hFind == INVALID_HANDLE_VALUE)
{
   AfxMessageBox("遍历文件失败");
   return;
}
else
{
   while(::FindNextFile(hFind,&findData)!=0)
   {
strFileName = findData.cFileName;
if(strFileName=="." || strFileName=="..")
continue;

//如果是目录,则不读写,这里没有做递归处理
if(findData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
continue;

//如果是正常文件,则读取文件并分析
fu_Read(strPath + strFileName);
   }
   ::FindClose(hFind);
}
}
// 读取文件内容,strFileName是文件名
void CFindEmailDlg::fu_Read(CString strFileName)
{

HANDLE hOpen;
CString strMsg;
CString strRead;
char *pBuf;
DWORD dwReadLen;
DWORD dwFileLen;
hOpen = ::CreateFile(strFileName,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if ( INVALID_HANDLE_VALUE==hOpen)
{
   ::CloseHandle(hOpen);
   strMsg = "打开文件失败:" + strFileName;
   AfxMessageBox(strMsg);
   return;
}
//读取文件内容
dwFileLen = ::GetFileSize(hOpen,NULL); //获取文件长度
//给读缓冲区分配内存
pBuf = new char[dwFileLen+1];
memset(pBuf,0,dwFileLen+1);
::ReadFile(hOpen,pBuf,dwFileLen,&dwReadLen,NULL);
strRead = pBuf;
delete[] pBuf;
::CloseHandle(hOpen);
//调用分析函数
fu_fx(strRead);
//AfxMessageBox(strRead);
}
// 分析读取的数据,邮件名称以制表符(/t)分隔
void CFindEmailDlg::fu_fx(CString strRead)
{
CString strMail;
CString strTemp;
DWORD dwPos,dwLen;

strRead+="/t";
dwLen = strRead.GetLength();
strMail="";
for(dwPos=0;dwPos < dwLen;dwPos++)
{
   strTemp = strRead.Mid(dwPos,1);
   //如果是回车符或换行符,则不操作
   if(strTemp=="/r" || strTemp=="/n")
continue;
   //如果是/t制表符,则截取邮件名称
   if(strTemp=="/t")
   {
//调用统计函数
fu_tj(strMail);
strMail="";
   }
   else
   {
strMail+=strTemp;
   }
}
}
// 根据邮件名称进行统计
void CFindEmailDlg::fu_tj(CString strMail)
{
DWORD dwCount,dwFind;
CString strText,strTj;
int nTj;
BOOL bFind;
dwCount = m_list1.GetItemCount();
bFind = FALSE; //如果没在列表中找到,bFind=FALSE,如果在列表中已找到,bFind=TRUE
for(dwFind=0;dwFind<dwCount;dwFind++)
{
   //获取列表中已存在的文件名
   strText = m_list1.GetItemText(dwFind,0); //列表中已存在的邮件名名
   strTj = m_list1.GetItemText(dwFind,1); //计数值
   if(strText==strMail)
   {
//如果已在列表中存在,则计数值加1
bFind = TRUE;
nTj = atoi(strTj);
nTj++;
strTj.Format("%d",nTj);
m_list1.SetItemText(dwFind,1,strTj);
   }
}
//如果不存在,则添加
if(!bFind)
{
   dwFind = m_list1.InsertItem(m_list1.GetItemCount(),strMail);
   m_list1.SetItemText(dwFind,1,"1"); //新增的,统计次数为1
}
}