Reputation: 981
I've been trying to create a folder recursively, meaning when I input
"C:\\test\\aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaabaababbaabababaabbababababaabababbabab\\22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222\\12345");
it would create every folder that isn't there up until the last entry. I've tried SHCreateDirectoryExW, but that one does not accept paths longer than MAX_PATH, not even with the special prefix. Is there an already existing function that does this with up to 32k characters or do I have to create one myself? I can create a normal folder, given that the folders above exist, just fine.
Upvotes: 2
Views: 2342
Reputation: 981
In the end I've succumbed to Boost, mainly because it is only 1 line I needed to add in.
std::tstring widePath = WIDEPATH_PREFIX;
widePath += CStringW(filePath.c_str());
return (boost::filesystem::create_directories(widePath));
I simply attached the prefix to my filepath and sent it off to boost.
Upvotes: 0
Reputation: 613003
You'll need to create each directory on the path individually. Parse the path into its directory components, and then work through them one by one. When you encounter a directory that does not exist, create it, and move on to the next level down.
The API function that you need to use here is CreateDirectory
. Pass it the full path to the directory that you need to create. You'll need to use the Unicode version of the API, with the \\?\
prefix to disable the MAX_PATH
length limit.
Upvotes: 5
Reputation: 409196
Split the path into its components. Check that each of these components exists, or create then one by one, while setting the applications current directory to the directory. This way you don't deal with the complete path, only one (relative) component at a time.
So something like this pseudo-code:
split_path();
set_cwd(root);
for (i = 0; i < number_of_components; ++i)
{
if (!does_path_exits(component[i]))
makedir(component[i]);
set_cwd(component);
}
Upvotes: 0