MFC中CFileFind类的应用
在我们的生活中,我们经常碰到“新建工程或新建文件”的情形,
你知道这样的应用背后是怎么来判断你的新建文件名在你选择的路径下是否可行(没有重名)呢??
函数功能:判断指定的目录下是否有同名的工程和同名的文件
void CuserfilefindDlg::On32771_newproject()
{
// TODO: Add your command handler code here
CFileFind ff;
CString m_Edit2Val="G:\\wuproject";
CString m_Edit1Val="wu1";
CString szDir=m_Edit2Val;//保存工程的路径
if(szDir.Right(1) != "\\")
szDir += "\\";
//--------------------------判断当前目录下是否有同名的工程和文件
szDir += "*.*";
BOOL res = ff.FindFile(szDir);
while(res)
{
res = ff.FindNextFile();
if(ff.IsDirectory() && !ff.IsDots()) //判断是否为目录
{
CString strPath = ff.GetFilePath();//得到全路径
AfxMessageBox(strPath);
//CString strTitle = ff.GetFileTitle();
CString strTitle = ff.GetFileName();
AfxMessageBox(strPath);
if(StrCmp(m_Edit1Val,strTitle)==0)
{
AfxMessageBox(_T("当前路径已存在名称为")+strTitle+_T("的工程,\n请重新定义工程名"));
return;
}
}
else if(!ff.IsDirectory() && !ff.IsDots())
{
//显示当前访问的文件
CString strPath;
CString strTitle;
strPath = ff.GetFilePath();
strTitle = ff.GetFileTitle();
if(StrCmp(m_Edit1Val,strTitle)==0)
{
AfxMessageBox(_T("当前路径已存在名称为")+strTitle+_T("的文件,\n请重新定义工程名"));
return;
}
}
}