How do I recursively create a folder in Win32?

前端 未结 14 1306
孤城傲影
孤城傲影 2020-12-01 14:11

I\'m trying to create a function that takes the name of a directory (C:\\foo\\bar, or ..\\foo\\bar\\..\\baz, or \\\\someserver\\foo\\bar

14条回答
  •  Happy的楠姐
    2020-12-01 14:15

    For Windows XP and up. Expects widechar null terminated string and amount of recursive actions as parameters. Was not tested with more than 1 level yet.

    Note: Path seperators must be '\'

    bool CreateRecursiveDirectoryW(const wchar_t* filepath, const int max_level)
    {
        bool result = false;
        wchar_t path_copy[MAX_PATH] = {0};
        wcscat_s(path_copy, MAX_PATH, filepath);
        std::vector path_collection;
    
        for(int level=0; PathRemoveFileSpecW(path_copy) && level < max_level; level++)
        {
            path_collection.push_back(path_copy);
        }
        for(int i=path_collection.size()-1; i >= 0; i--)
        {
            if(!PathIsDirectoryW(path_collection[i].c_str()))
                if(CreateDirectoryW(path_collection[i].c_str(), NULL))
                    result = true;
        }
        return result;
    };
    

提交回复
热议问题