Recursively create a directory tree (multilevel directory)

Source: Internet
Author: User

As if the system provided only the creation of a directory function, but a function to create a multilevel directory was not found, so I wrote one, for reference only.

This code is based on C + + Builder


void __fastcall makedirectory (ansistring Dir)//Create a complex directory
{
Recursively create multilevel catalogs
When a level of directory failure is created, retire a level directory, create a directory, and so on, until the directory is created successfully, and then exit recursively, and the last creation failed directory creation completes

Non-Multilevel directory format
if (Dir.length () <= 3)
{
Return
}

Change path to directory form
if (Dir[dir.length ()] = = =///change path to Directory
{
Dir.delete (Dir.length (), 1);
}

Modify file properties
Win32_find_data WFD;
HANDLE Hfind=findfirstfile (Dir.c_str (), &WFD); Find
if (hfind!= invalid_handle_value)
{
FindClose (hfind);

if (Wfd.dwfileattributes & File_attribute_directory)
Return
}

Failed to create the directory for the current directory
if (Createdir (Dir) = = False)
{
Back level Directory
ansistring newdir = Dir;
while (Newdir[newdir.length ()]!= ' \ \]//To the previous directory in the current directory
{
Newdir.delete (Newdir.length (), 1);
}
Delete symbol \ \
Newdir.delete (Newdir.length (), 1); Will//also delete

Recursive entry
Makedirectory (Newdir); Recursive this function, and then create a table of contents
Directories that failed before they were created recursively exited
Createdir (Dir); Recursive return, building the directory on the existing directory
}
When finished, the directory tree was successfully established.
}
//---------------------------------------------------------------------------

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.