C++ mkdir 创建多级文件夹目录

时间:2022-12-14 12:30:09

mkdir函数的头文件:

#include <direct.h>

用for循环依次创建:

    char *strDirPath="D:\\test\\test1\\test2\\";
    if (strlen(strDirPath)>MAX_PATH)
    {
        return;
    }
    int ipathLength=strlen(strDirPath);
    int ileaveLength=0;
    int iCreatedLength=0;
    char szPathTemp[MAX_PATH]={0};
    for (int i=0;(NULL!=strchr(strDirPath+iCreatedLength,'\\'));i++)
    {
        ileaveLength=strlen(strchr(strDirPath+iCreatedLength,'\\'))-1;
        iCreatedLength=ipathLength-ileaveLength;
        strncpy(szPathTemp,strDirPath,iCreatedLength);
        mkdir(szPathTemp);
    }
    if (iCreatedLength<ipathLength)
    {
        mkdir(strDirPath);
    }