/////////////////////////////////////////////////////////////// // // // nav.cpp - recursively walk directory tree // // ver 1.2 starting at a specified path // // // // Language: Visual C++, ver 5.0 // // Platform: Micron Dual Pentium 200, Windows NT 4.0 // // Application: CRI-OOD Project #1, S99 // // Author: Jim Fawcett, CST 2-187, (315) 443-3948 // // fawcett@ecs.syr.edu // // // /////////////////////////////////////////////////////////////// #include // names and paths #include // win32 directory services #include // win32 file services #include // standard output for test #include // output formatting #include "nav.h" using namespace std; //----< default dir name display function >-------------------- void dProc::dirsProc(const char *dirName) { cout << "\n " << dirName << endl; } //----< default file data display function >------------------- void dProc:: fileProc(const WIN32_FIND_DATA *findData) { DWORD size = /*findData->nFileSizeHigh*MAXDWORD + */ findData->nFileSizeLow; FILETIME ft; FileTimeToLocalFileTime(&(findData->ftLastWriteTime),&ft); SYSTEMTIME st; FileTimeToSystemTime(&ft,&st); cout << '\t'; cout.setf(ios::left,ios::adjustfield); cout << setw(15) << findData->cFileName; cout.setf(ios::right,ios::adjustfield); cout << setw(10) << size << " "; cout << setw(2) << st.wMonth << "/" << setw(2) << st.wDay << "/" << setw(4) << st.wYear << " " << setw(2) << st.wHour << ":" << setw(2) << st.wMinute; cout << endl; } // //----< save user's working directory >------------------------ navig::navig() { // save user's working directory char buffer[256]; GetCurrentDirectory( 256, buffer); userDir = buffer; p = new dProc; } //----< use alternate display processor >---------------------- void navig::use(dProc *dp) { delete p; p = dp; } //----< restore user's working directory >--------------------- navig::~navig() { SetCurrentDirectory(userDir.c_str()); delete p; } //----< specify starting directory and initiate walk >--------- void navig::start(string dir) { // convert "." or ".." into full path name char buffer[256]; if( ( dir == "." ) || ( dir == ".." ) ) { if( !SetCurrentDirectory( dir.c_str() ) ) cout << "could not find dir " << dir << endl; GetCurrentDirectory( 256, buffer); dir = buffer; } // start recursive directory walk walk(dir); } // //----< walk directory tree rooted at dir >-------------------- void navig::walk(const string &dir) { string fileMask = "*.*"; char *fileName; char curDir[ 256 ]; char fullName[ 256 ]; HANDLE fileHandle; WIN32_FIND_DATA findData; // save current dir so can restore it if( !GetCurrentDirectory( 256, curDir) ) return; // if the directory name is neither . or .. then // change to it, otherwise ignore it if( (dir != "." ) && (dir != ".." ) ) { if( !SetCurrentDirectory( dir.c_str() ) ) return; } else return; // print out the current directory name if( !GetFullPathName( fileMask.c_str(), 256, fullName, &fileName ) ) return; p->dirsProc(fullName); // Loop through all files in the directory fileHandle = FindFirstFile( fileMask.c_str(), &findData ); while ( fileHandle != INVALID_HANDLE_VALUE ) { // If the name is a directory, // recursively walk it. Otherwise print // print the file's data if( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) walk( findData.cFileName ); else p->fileProc(&findData); // loop thru remaining entries in the dir if (!FindNextFile( fileHandle, &findData )) break; } // clean up and restore directory FindClose( fileHandle ); SetCurrentDirectory( curDir ); p->dirsProc(curDir); } // //----< test stub >-------------------------------------------- #ifdef TEST_NAV void main(int argc, char *argv[]) { if(argc == 1) { cout << " please enter starting path\n"; return; } navig nav; nav.start(argv[argc-1]); } #endif