常用函数之检测目录、多级目录创建

编写代码中经常回遇到一些常用的函数,这些函数并不是很容易用脑袋瓜子记录下来,为了以后更方便的使用且不再去做无用功,收集的函数暂以博客的形式记录下来。

///检测一个目录是否存在,存在返回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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值