Search code examples
c++cwinapidirectorycreation

Are there any function in C or C++ or WinApi to create directory including all unexistent directories in specified path?


I need an analog of c#

Directory.CreateDirectory("d:\\asd\\dsa\\123");

which will create all that directories, even if disk D is totally empty with no any directories.

I read about WinApi CreateDirectory next thing:
"ERROR_PATH_NOT_FOUND - One or more intermediate directories do not exist; this function will only create the final directory in the path."
So it's not what I looking for..

Any other ways to do what I want?


Solution

  • Did you try to use mkdir() function ? Another way to use:

    1. boost filesystem: supports standard MAX_PATH size 260.

      const char dir_path[] = "c:\\temp\\cplusplus";

      boost::filesystem::path dir(dir_path);
              if(boost::filesystem::create_directory(dir)) {
                  std::cout << "Success" << "\n";
              }
      
    2. SHCreateDirectoryEx function for Win XP(SP2) and Higher. However, it is limited to 247 characters, which is less than the standard MAX_PATH (260) that other Win32 API filesystem functions support

    3. CreateDirectory function : default string size limit for paths of 248 characters. This limit is related to how the CreateDirectory function parses paths. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" prefix to the path.

    NOTE: Because most Boost.Filesystem operational functions just pass the contents of a class path object to the Windows API, they do work with the extended-length prefixes. But some won't work, because to the limitations imposed by Windows. -- Boost warning.