`

封装的createDirectories方法

    博客分类:
  • C++
阅读更多
#include <io.h>
#include <afx.h>
BOOL fnCreateDirectory(CString csFilePath, CString &csErr)
{
if(csFilePath.IsEmpty())
{
csErr.Format("directory argument is empty\r\n");
return FALSE;
}

if(-1 != _access(csFilePath, 0))
{
return TRUE;
}

int pos = 0;
CString csLeft;
CString csRight;
CStringArray casPaths;

csFilePath.Replace("/", "\\");
while(true)
{
pos = csFilePath.ReverseFind('\\');
if(-1 == pos)
{
break;
}

csLeft = csFilePath.Left(pos);
csRight = csFilePath.Right((csFilePath.GetLength() - pos -1));

if(!csRight.IsEmpty())
{
casPaths.Add(csRight);
}

if(-1 != _access(csLeft, 0))
{
break;
}
csFilePath = csLeft;
}

CString csTempPath = csLeft;
int size = casPaths.GetUpperBound();
CString csTem;
for( ; size>-1 ;size--)
{
if(casPaths.GetAt(size).IsEmpty())
{
continue;
}
csTem = '/' + casPaths.GetAt(size);
csTempPath = csTempPath + csTem;

if(0 == CreateDirectory(csTempPath, NULL))
{
csErr.Format("create directory %s failed\r\n", csTempPath);
return FALSE;
}
csErr.Format("create directory %s failed\r\n", csTempPath);
}
return TRUE;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics