自动创建文件目录

闲暇时间,整理下自动创建文件目录的相关流程


bool Inner_IsExistsPath(const char* path)
{
    DWORD fa = GetFileAttributes(path);
    return (fa != (DWORD)-1);
}
 
bool Inner_CreatePath(const char* path)
{
    return ::CreateDirectory(path, NULL);
}
 
bool CreateFullPath(const char* fullPath)
{
    if (fullPath == NULL || fullPath[0] == 0)
    {
        return false;
    }
 
    int pos = -1;
    for (int i = strlen(fullPath) - 1; i >= 0; i--)
    {
        if (fullPath[i] == '\\' || fullPath[i] == '/')
        {
            pos = i;
            break;
        }
    }
 
    if (pos == -1)
    {
        return false;
    }
 
    char strPath[MAX_PATH] = { 0 };
    strncpy(strPath, fullPath, pos);
    strPath[pos + 1] = '\0';
 
    if (Inner_IsExistsPath(strPath))
    {
        return true;
    }
 
    vector<string> vDir;
    char * p = strPath;
    bool bFind = false;
    while (strlen(p) > 0)
    {
        bFind = false;
        for (int i = 0; i < strlen(p); i++)
        {
            if (p[i] == '\\' || p[i] == '/')
            {
                p[i] = '\0';
                vDir.push_back(p);
                p += i + 1;
                bFind = true;
                break;
            }
        }
 
        if (!bFind)
        {
            vDir.push_back(p);
            break;
        }
    }
 
    if (vDir.empty())
    {
        return false;
    }
 
    string currPath = "";
    for (int i = 0; i < vDir.size(); i++)
    {
        if (i == 0)
        {
            currPath = vDir[i];
        }
        else
        {
            currPath += "/" + vDir[i];
        }
 
        if (Inner_IsExistsPath(currPath.c_str()))
        {
            continue;
        }
 
        if (!Inner_CreatePath(currPath.c_str()))
        {
            return false;
        }
    }
 
    return true;
}

bool Inner_IsExistsPath(const char* path)
{
    DWORD fa = GetFileAttributes(path);
    return (fa != (DWORD)-1);
}
 
bool Inner_CreatePath(const char* path)
{
    return ::CreateDirectory(path, NULL);
}
 
bool CreateFullPath(const char* fullPath)
{
    if (fullPath == NULL || fullPath[0] == 0)
    {
        return false;
    }
 
    int pos = -1;
    for (int i = strlen(fullPath) - 1; i >= 0; i--)
    {
        if (fullPath[i] == '\\' || fullPath[i] == '/')
        {
            pos = i;
            break;
        }
    }
 
    if (pos == -1)
    {
        return false;
    }
 
    char strPath[MAX_PATH] = { 0 };
    strncpy(strPath, fullPath, pos);
    strPath[pos + 1] = '\0';
 
    if (Inner_IsExistsPath(strPath))
    {
        return true;
    }
 
    vector<string> vDir;
    char * p = strPath;
    bool bFind = false;
    while (strlen(p) > 0)
    {
        bFind = false;
        for (int i = 0; i < strlen(p); i++)
        {
            if (p[i] == '\\' || p[i] == '/')
            {
                p[i] = '\0';
                vDir.push_back(p);
                p += i + 1;
                bFind = true;
                break;
            }
        }
 
        if (!bFind)
        {
            vDir.push_back(p);
            break;
        }
    }
 
    if (vDir.empty())
    {
        return false;
    }
 
    string currPath = "";
    for (int i = 0; i < vDir.size(); i++)
    {
        if (i == 0)
        {
            currPath = vDir[i];
        }
        else
        {
            currPath += "/" + vDir[i];
        }
 
        if (Inner_IsExistsPath(currPath.c_str()))
        {
            continue;
        }
 
        if (!Inner_CreatePath(currPath.c_str()))
        {
            return false;
        }
    }
 
    return true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值