#include <iostream>
#include <windows.h>
#include <string>
#include <cstring>
#include <new>
using namespace std;
//initializations
int numFolders = 0;
int numSubnodes = 0;
HANDLE h;
WIN32_FIND_DATA fd;
struct node
{
string value;
node ** subnode;
}; //end node
void createtree(int n, node * a)
{
//The integer parameter is only going to be used what to store to values. It will also provide for easy access to the parent value:
//parent_value == a->value
if (n == 0)
{
a->value = "C:/Users/owner/Desktop"; //store the root directory to the value.
cout << a->value << endl;
h = FindFirstFile("C:/Users/owner/Desktop/*.", &fd); //set the handle to FindFirstFile(root_directory, &fd)
//Find number of folders in root directory. Exclude any that have '.' in the name.
do
{
if (((string)fd.cFileName != ".") && ((string)fd.cFileName != ".."))
{
numSubnodes++;
} //end if
} //end do
while (FindNextFile(h,&fd));
if (numSubnodes != 0)
{
createtree(1,a);
} //end inner if
} //end if
else
{
if (numSubnodes != 0)
{
a->subnode = new(nothrow) node * [numSubnodes]; //make array of subnode pointers
numFolders = numSubnodes; //passing the number of folders to another variable (to prevent logic error in the loop)
cout << "numFolders == "<<numFolders<<endl;
cout << "a->value == "<<a->value<<endl;
h = FindFirstFile((a->value+"/*.").c_str(), &fd); //set the handle to FindFirstFile(value_of_parent_directory, &fd)
for (int temp = 0; temp < numFolders; temp++)
{
a->subnode[temp] = new (nothrow) node;
//Set the value to fd.cFileName. Use control structure here to check for the "." and ".." (to throw them out)
if (temp == 0)
{
while ((((string)fd.cFileName == ".") || ((string)fd.cFileName == "..")) && (FindNextFile(h,&fd)))
{
//do nothing; the FindNextFile(h,&fd) is changing fd.cFileName to the next folder
} //end while
//This is just in case the only subdirectories found were "." and ".."
if (((string)fd.cFileName != ".") && ((string)fd.cFileName != ".."))
{
a->subnode[temp]->value = (string)fd.cFileName;
} //end if
} //end if
else
{
if (FindNextFile(h,&fd)) a->subnode[temp]->value = (string)fd.cFileName;
} //end else
//append the parent value to the front of them
if ((string)fd.cFileName != "")
{
a->subnode[temp]->value = (string)a->value+'/'+a->subnode[temp]->value;
cout << a->subnode[temp]->value << endl;
} //end if
} //end for
for (int temp = 0; temp < numFolders; temp++)
{
numSubnodes = 0; //reset the number of subnodes to 0
h = FindFirstFile((a->subnode[temp]->value + "/*.").c_str(), &fd); //Initializing the handle value for the next run
//check the number of folders (that aren't named "." or ".."!)
do
{
if (((string)fd.cFileName != ".")&&((string)fd.cFileName != ".."))
{
numSubnodes++;
} //end if
} //end do
while (FindNextFile(h,&fd));
createtree(n+1,a->subnode[temp]);
} //end for
} //end if
else
{
a->subnode = 0; //initialize the pointer to 0; there is nothing to be done here!
} //end else
} //end else
} //end createtree
int main()
{
node dirtree;
createtree(0,&dirtree);
return 0;
}
This post has been edited by IceHot: 31 December 2012 - 06:27 PM

New Topic/Question
Reply




MultiQuote



|