PRODUCT : Borland C++ NUMBER : 1742
VERSION : All
OS : All
DATE : December 7, 1993 PAGE : 1/3
TITLE : Recursive function to get directory information
/*
*
* RECURD.CPP: A CLASSLIB Stack-based directory tree function
* This is a reasonably short recursive function which uses
* the container class indirect Stack template class to
* collect and access the names of all the directories in a
* given directory tree.
³
*
* The main() takes a starting directory on the command line,
* uses the dirTreeCollector() function to load a dirTree
* stack and then accesses the 'dirTree' Stack object to
* display a crude directory tree.
*
*/
#include
// findfirst(), findnext()
#include // FA_DIREC
#include // sprintf()
#include // Stack
#include // BI_IStackAsVector
#include // String
/////////////////////////////////////////// DIRENT /////////////
struct DIRENT
{
int level;
String *s;
};
typedef BI_IStackAsVector DIRTREE;
typedef BI_IStackAsVectorIterator DIRTREEITERATOR;
DIRTREE dirTree(400);
/*--------------------------------------------------------------
*/
extern unsigned _stklen = 16000;
void dirTreeCollector(const char *direct)
{
ffblk ffb;
PRODUCT : Borland C++ NUMBER : 1742
VERSION : All
OS : All
DATE : December 7, 1993 PAGE : 2/3
TITLE : Recursive function to get directory information
char buf1[260];
char buf2[260];
int done;
static level = 0;
// Level 1 is the lowest level. Count ourselves called.
++level;
// Compose an "all files" spec from the base path
// passed by caller.
sprintf(buf1, "%s\\*.*", direct);
// See if there are any directories in here.
done = findfirst(buf1, &ffb, FA_DIREC);
DIRENT *dent;
while (!done)
{
// If this is a directory and it isn't one those phony
// dot directories...
if (ffb.ff_attrib == FA_DIREC)
{
if (*ffb.ff_name != '.')
{
// ...build the new directory path,...
sprintf(buf2, "%s\\%s", direct, ffb.ff_name);
// ...give us a new DIRectry ENTry object,
// initialize it,...
dent = new DIRENT;
dent->level = level;
dent->s = new String(buf2);
// ...put it on the Stack and...
dirTree.push(dent);
// ...call ourselves 'till we can't go any deeper.
dirTreeCollector(buf2);
}
}
PRODUCT : Borland C++ NUMBER : 1742
VERSION : All
OS : All
DATE : December 7, 1993 PAGE : 3/3
TITLE : Recursive function to get directory information
// Hey we're back. Let's see if there's another tree
// we can explore.
done = findnext(&ffb);
};
// If not we drop a level and continue this process until
// there are no directories left at the bottom of the tree.
--level;
} // end of dirTreeCollector()
//**************************************************************
void main(int argc, char *argv[])
{
int dcount;
if (argc > 1)
{
dirTreeCollector(argv[1]);
dcount = dirTree.getItemsInContainer();
cout << "\nThere are " << dcount << " directories in the "
<< argv[1] << " directory tree.";
while (!dirTree.isEmpty())
{
cout << endl;
DIRENT *dent = dirTree.pop();
for (int i=0; ilevel; i++) cout << " ";
cout << *dent->s;
}
}
} // end of main()
DISCLAIMER: You have the right to use this technical information
subject to the terms of the No-Nonsense License Statement that
you received with the Borland product to which this information
pertains.