#include #include #include "apstring.h" #include "directory.h" // Owen Astrachan, 4/18/95, 5/10/99 // print all entries in a directory (uses recursion) void Tab(int count) // postcondition: count tabs printed to cout { int k; for(k=0; k < count; k++) { cout << "\t"; } } void ProcessDir(const apstring & path, int tabCount) // precondition: path specifies pathname to a directory // tabCount specifies how many tabs for printing // postcondition: all files and subdirectories in directory 'path' // printed, subdirectories tabbed over 1 more than parent { DirStream indir(path); DirEntry entry; int num = 0; // number of files in this directory if (! indir.fail()) // directory opened successfully? { for(indir.Init(); indir.HasMore(); indir.Next()) { entry = indir.Current(); // either file or subdirectory // don't process self: ".", or parent directory: ".." if (entry.Name() != "." && entry.Name() != "..") { num++; Tab(tabCount); // print spaces cout << "(" << setw(4) << num << ") " << entry.Size() << "\t" << entry.Name() << endl; // and name if (entry.IsDir() ) // process subdir { ProcessDir(entry.Path(),tabCount+1); } } } } // end if (! fail) } int main() { apstring dirname; cout << "enter directory name "; cin >> dirname; ProcessDir(dirname,0); return 0; }