根据目录路径递归创建目录

来源:互联网 发布:淘宝卫浴好店 编辑:程序博客网 时间:2024/05/18 03:41

问题描述:

给定一个目录路径,如果该路径不存在则创建出来。

参考代码:

void CorrectPath(CString& strPath){    strPath.Replace(_T('/'), _T('\\'));    do     {        if (strPath.Find(_T("\\\\")) == -1)            break;        strPath.Replace(_T("\\\\"), _T("\\"));    } while (true);}void PathRemoveFileSpec(CString& strPath){    CorrectPath(strPath);    int nPos = strPath.ReverseFind(_T('\\'));    if (nPos == -1)    {        strPath.Empty();    }    else    {        strPath = strPath.Left(nPos);    }}BOOL CreateDeepDirectory(LPCTSTR szPath){    BOOL bRetCode = FALSE;    CString strPath(szPath);    if (GetFileAttributes(szPath) != INVALID_FILE_ATTRIBUTES)        return TRUE;    bRetCode = ::CreateDirectory(szPath, NULL);    if (!bRetCode && ::GetLastError() != ERROR_ALREADY_EXISTS)    {        PathRemoveFileSpec(strPath);        if (strPath.IsEmpty()) return FALSE;        bRetCode = CreateDeepDirectory(strPath);        if (!bRetCode) return FALSE;        bRetCode = ::CreateDirectory(szPath, NULL);        if (!bRetCode && ::GetLastError() != ERROR_ALREADY_EXISTS)            return FALSE;    }    return TRUE;}int _tmain(int argc, _TCHAR* argv[]){    CreateDeepDirectory(L"C:\\abc\\def\\");    return 0;}
原创粉丝点击