编写代码中经常回遇到一些常用的函数,这些函数并不是很容易用脑袋瓜子记录下来,为了以后更方便的使用且不再去做无用功,收集的函数暂以博客的形式记录下来。
///检测一个目录是否存在,存在返回true,不存在返回false;
bool fnDirIsExist(const string &strPath)
{
WIN32_FIND_DATA wfd;
ZeroMemory(&wfd,sizeof(WIN32_FIND_DATA));
bool rValue = false;
HANDLE hFind = FindFirstFile(strPath.c_str(), &wfd);
if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
rValue = true;
}
FindClose(hFind);
return rValue;
}
//逐步创建多级目录,创建成功返回true,失败返回false;
bool fnCreateDir(CString &csDirPath)
{
CString csLeft,csRight;
int iPos = 0;
csRight = csDirPath;
while((iPos = csRight.Find('\\')) >= 0)
{
if(iPos > 0)
{
csLeft = csLeft + csRight.Left(iPos+1);
csRight.Delete(0,iPos+1);
if(!fnDirIsExist(csLeft))
{
if(!CreateDirectory(csLeft,NULL))
{
return false;
}
}
}
}
//检测是否还包含不带\的目录
if(!csRight.isEmpty())
{
csLeft = csLeft + csRight;
if(!fnDirIsExist(csLeft))
{
if(!CreateDirectory(csLeft,NULL))
{
return false;
}
}
}
return true;
}