void CWork2Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString str;//声明一个字符串
BROWSEINFO bi;
char name[MAX_PATH];
ZeroMemory(&bi,sizeof(BROWSEINFO));
bi.hwndOwner = GetSafeHwnd();
bi.pszDisplayName = name;
bi.lpszTitle = "Select folder";
//bi.ulFlags = BIF_USENEWUI;
bi.ulFlags = BIF_RETURNFSANCESTORS;
LPITEMIDLIST idl = SHBrowseForFolder(&bi);
if(idl == NULL)
return;
SHGetPathFromIDList(idl, str.GetBuffer(MAX_PATH));
str.ReleaseBuffer();
CString m_strPath;
m_strPath = str;//为对话框中与一编辑框对应的CString型变量,保存并显示选中的路径。
if(str.GetAt(str.GetLength()-1)!='\\')
m_strPath+="\\";
UpdateData(FALSE);
sourcePath=m_strPath;//把获取到选择的结果递给sourcePath
//AfxMessageBox(sourcePath);
//下面的代码是把编辑框的内容改为sourcePath
SetDlgItemText(IDC_EDIT1,sourcePath);
}
void CWork2Dlg::OnButton2()
{
// TODO: Add your control notification handler code here
CString str;//声明一个字符串
BROWSEINFO bi;
char name[MAX_PATH];
ZeroMemory(&bi,sizeof(BROWSEINFO));
bi.hwndOwner = GetSafeHwnd();
bi.pszDisplayName = name;
bi.lpszTitle = "Select folder";
//bi.ulFlags = BIF_USENEWUI;
bi.ulFlags = BIF_RETURNFSANCESTORS;
LPITEMIDLIST idl = SHBrowseForFolder(&bi);
if(idl == NULL)
return;
SHGetPathFromIDList(idl, str.GetBuffer(MAX_PATH));
str.ReleaseBuffer();
CString m_strPath;
m_strPath = str;//为对话框中与一编辑框对应的CString型变量,保存并显示选中的路径。
if(str.GetAt(str.GetLength()-1)!='\\')
m_strPath+="\\";
UpdateData(FALSE);
pictureSavePath=m_strPath;//把获取到选择的结果递给sourcePath
//AfxMessageBox(pictureSavePath);
SetDlgItemText(IDC_EDIT2,pictureSavePath);
}
void CWork2Dlg::OnButton3()
{
// TODO: Add your control notification handler code here
CString str;//声明一个字符串
BROWSEINFO bi;
char name[MAX_PATH];
ZeroMemory(&bi,sizeof(BROWSEINFO));
bi.hwndOwner = GetSafeHwnd();
bi.pszDisplayName = name;
bi.lpszTitle = "Select folder";
//bi.ulFlags = BIF_USENEWUI;
bi.ulFlags = BIF_RETURNFSANCESTORS;
LPITEMIDLIST idl = SHBrowseForFolder(&bi);
if(idl == NULL)
return;
SHGetPathFromIDList(idl, str.GetBuffer(MAX_PATH));
str.ReleaseBuffer();
CString m_strPath;
m_strPath = str;//为对话框中与一编辑框对应的CString型变量,保存并显示选中的路径。
if(str.GetAt(str.GetLength()-1)!='\\')
m_strPath+="\\";
UpdateData(FALSE);
indexSavePath=m_strPath;//把获取到选择的结果递给sourcePath
//AfxMessageBox(indexSavePath);
SetDlgItemText(IDC_EDIT3,indexSavePath);
}
void CWork2Dlg::OnButton4()
{
// TODO: Add your control notification handler code here
if(sourcePath.IsEmpty())
{
AfxMessageBox("请选择源文件路径");
return;
}
if(pictureSavePath.IsEmpty())
{
AfxMessageBox("请选择图片保存路径");
return;
}
if(indexSavePath.IsEmpty())
{
AfxMessageBox("请选择索引保存路径");
return;
}
else
{
//处理图片
CopyPictureFile();//处理复制
AfxMessageBox("复制完成");
DelectPictureFile(sourcePath);
AfxMessageBox("删除完成");//删除删除
//处理索引
SetIndexName();//设置索引文件名
CreateIndex();//生成索引
DelectHdrFile(sourcePath);//删除hdr文件
}
}
void CWork2Dlg::CopyPictureFile()
{
//如果写成CopyFile会跟系统函数重名
FileCopy(sourcePath,pictureSavePath,"*.bmp");
FileCopy(sourcePath,pictureSavePath,"*.jpg");
FileCopy(sourcePath,pictureSavePath,"*.png");
FileCopy(sourcePath,pictureSavePath,"*.gif");
}
void CWork2Dlg::FileCopy(CString source, CString destination, CString searchStr)
{
CString strSourcePath = source;//源路径
CString strDesPath = destination; //目的路径
CString strFileName = searchStr; //文件名的字符串
CFileFind filefinder;
CString strSearchPath = strSourcePath + "\\" + strFileName;
CString filename;
BOOL bfind = filefinder.FindFile(strSearchPath);
CString SourcePath, DisPath;
while (bfind)
{
bfind = filefinder.FindNextFile();
filename = filefinder.GetFileName();
SourcePath = strSourcePath + "\\" + filename;
DisPath = strDesPath + "\\" + filename;
//如果有名称相同的那么覆盖。
CopyFile((LPCSTR)SourcePath, (LPCSTR)DisPath, TRUE);
}
filefinder.Close();
}
void CWork2Dlg::DelectPictureFile(CString sDirName)
{
CFileFind tempFind;
char sTempFileFind[200] ;
sprintf(sTempFileFind,"%s\\*.*",sDirName); //将第二个参数写入到第一个参数当中
BOOL IsFinded = tempFind.FindFile(sTempFileFind);
while (IsFinded)
{
IsFinded = tempFind.FindNextFile();
//当这个目录中不含有.的时候,。
if (!tempFind.IsDots()&&!tempFind.IsDirectory())
{
CString fileNameSuffix=tempFind.GetFileName().Right(3);
if(fileNameSuffix.Find("bmp")>=0
||fileNameSuffix.Find("png")>=0
||fileNameSuffix.Find("jpg")>=0
||fileNameSuffix.Find("gif")>=0)
{
char sFoundFileName[200];
strcpy(sFoundFileName,tempFind.GetFileName().GetBuffer(200));
char sTempFileName[200];
sprintf(sTempFileName,"%s\\%s",sDirName,sFoundFileName);
DeleteFile(sTempFileName);
}
}
}
tempFind.Close();
}
void CWork2Dlg::SetIndexName()
{
CTime tm = CTime::GetCurrentTime();
CString str2;
str2.Format(_T("%d_%d_%d__%d_%d_%d.txt"),tm.GetYear(),tm.GetMonth(),tm.GetDay(),tm.GetHour(),tm.GetMinute(),tm.GetSecond());
indexSaveName=str2;
}
void CWork2Dlg::CreateIndex()
{
int indexNum=0;
//查找hdr文件
CFileFind finder;
BOOL bWorking = finder.FindFile(sourcePath+"*.hdr");//++++++++++++++++++++++++++++++
while(bWorking)
{
bWorking=finder.FindNextFile();
//获取有用的信息
CString sip;CString spt;CString dip;CString dpt;CString ctm;
CString pictureName=finder.GetFileTitle();//文件路径先这样做。
CStdioFile read;
if(!read.Open(finder.GetFilePath(),CFile::modeRead))
{
AfxMessageBox("Open file error!");
return;
}
CString line;
while(read.ReadString(line))
{
int start=line.Find(":");
int end=line.Find(";");
CString val=line.Mid(start+2,end-6);
CString fre=line.Left(4);
if(fre.Compare("_sip")==0)
{
sip=val;//AfxMessageBox("sip:"+val);
}
else if(fre.Compare("_spt")==0)
{
spt=val;//AfxMessageBox("spt:"+val);
}
else if(fre.Compare("_dip")==0)
{
dip=val;//AfxMessageBox("dip:"+val);
}
else if(fre.Compare("_dpt")==0)
{
dpt=val;//AfxMessageBox("dpt:"+val);
}
else if(fre.Compare("_ctm")==0)
{
ctm=val;//AfxMessageBox("ctm:"+val);
}
}
read.Close();
//写信息到索引文件
//打开索引文件
CStdioFile file;
if(!file.Open(indexSavePath+indexSaveName,CFile::modeReadWrite))
{
file.Open(indexSavePath+indexSaveName,CFile::modeCreate|CFile::modeReadWrite);
}
file.SeekToEnd();
char strToWrite[3000];
sprintf(strToWrite,
"[%d]\nproto=\nstarttime=%s\nusername=\nuserip=%s\nuserport=%s\nserverip=%s\nserverport=%s\nrequest=\nfiletype=\nencoding=\ncharset=\nurl=\nhost=\nreferer=\nrefererhost=\nfilesize=\nreuse=0\nfilename=%s\nphone=\n",
indexNum,ctm,sip,spt,dip,dpt,pictureSavePath+pictureName);
indexNum++;
file.WriteString(strToWrite);
file.Close();
}
//最后在索引中写入system信息和count信息
CStdioFile file;
if(!file.Open(indexSavePath+indexSaveName,CFile::modeReadWrite))
{
file.Open(indexSavePath+indexSaveName,CFile::modeCreate|CFile::modeReadWrite);
}
file.SeekToEnd();
char strToWrite[3000];
sprintf(strToWrite,"[system]\ncount=%d",indexNum);
file.WriteString(strToWrite);
file.Close();
AfxMessageBox("处理索引文件完成");
}
void CWork2Dlg::DelectHdrFile(CString sDirName)
{
//传递过来的参数是CString类型的,但是我们现在需要把他转化为基本的字符串类型
CFileFind tempFind;
char sTempFileFind[200] ;
sprintf(sTempFileFind,"%s\\*.*",sDirName);
BOOL IsFinded = tempFind.FindFile(sTempFileFind);
while (IsFinded)
{
IsFinded = tempFind.FindNextFile();
//当这个目录中不含有.的时候,。
if (!tempFind.IsDots()&&!tempFind.IsDirectory())
{
CString fileNameSuffix=tempFind.GetFileName().Right(3);
if(fileNameSuffix.Find("hdr")>=0 )
{
char sFoundFileName[200];
strcpy(sFoundFileName,tempFind.GetFileName().GetBuffer(200));
char sTempFileName[200];
sprintf(sTempFileName,"%s\\%s",sDirName,sFoundFileName);
DeleteFile(sTempFileName);
}
}
}
tempFind.Close();
}
MFC
最新推荐文章于 2023-02-04 11:03:14 发布