Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a function I wrote which iteratively creates a folder tree. Here is the main function:</p> <pre><code>#include &lt;io.h&gt; #include &lt;string&gt; #include &lt;direct.h&gt; #include &lt;list&gt; // Returns false on success, true on error bool createFolder(std::string folderName) { list&lt;std::string&gt; folderLevels; char* c_str = (char*)folderName.c_str(); // Point to end of the string char* strPtr = &amp;c_str[strlen(c_str) - 1]; // Create a list of the folders which do not currently exist do { if (folderExists(c_str)) { break; } // Break off the last folder name, store in folderLevels list do { strPtr--; } while ((*strPtr != '\\') &amp;&amp; (*strPtr != '/') &amp;&amp; (strPtr &gt;= c_str)); folderLevels.push_front(string(strPtr + 1)); strPtr[1] = 0; } while (strPtr &gt;= c_str); if (_chdir(c_str)) { return true; } // Create the folders iteratively for (list&lt;std::string&gt;::iterator it = folderLevels.begin(); it != folderLevels.end(); it++) { if (CreateDirectory(it-&gt;c_str(), NULL) == 0) { return true; } _chdir(it-&gt;c_str()); } return false; } </code></pre> <p>The <code>folderExists</code> routine is as follows:</p> <pre><code>// Return true if the folder exists, false otherwise bool folderExists(const char* folderName) { if (_access(folderName, 0) == -1) { //File not found return false; } DWORD attr = GetFileAttributes((LPCSTR)folderName); if (!(attr &amp; FILE_ATTRIBUTE_DIRECTORY)) { // File is not a directory return false; } return true; } </code></pre> <p>An example call I tested the above functions with is as follows (and it works):</p> <pre><code>createFolder("C:\\a\\b\\c\\d\\e\\f\\g\\h\\i\\j\\k\\l\\m\\n\\o\\p\\q\\r\\s\\t\\u\\v\\w\\x\\y\\z"); </code></pre> <p>This function hasn't gone through very thorough testing, and I'm not sure it yet works with other operating systems (but probably is compatible with a few modifications). I am currently using <code>Visual Studio 2010</code> with <code>Windows 7.</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload