/////////////////////////////////////////////////////////////// // dir1.cpp - list current directory's contents // // // // Adapted by Jim Fawcett from the book: // // "Win32 System Services: // // The Heart of Windows 95 and Windows NT", Marshall Brain, // // Prentice Hall, Copyright 1995 // // // /////////////////////////////////////////////////////////////// #include #include #include #include using namespace std; //----< Prints data in findData >------------------------------ void PrintFindData(WIN32_FIND_DATA *findData) { // If it's a directory, print the name if( findData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { // use strstream to format output into one string // for insertion into specified field width ostrstream ss; ss << "<" << findData->cFileName << ">" << ends; cout << setw(20) << ss.rdbuf()->str() << endl; } // else if it's a file, print name and size else { cout << setw(20) << findData->cFileName; cout << setw(8) << findData->nFileSizeLow << endl; } } //----< Lists the contents of the current directory >---------- void ListDirectoryContents(char *fileMask) { HANDLE fileHandle; WIN32_FIND_DATA findData; // get first file fileHandle = FindFirstFile( fileMask,&findData ); if( fileHandle != INVALID_HANDLE_VALUE ) { PrintFindData( &findData ); // loop on all remeaining entries in dir while( FindNextFile( fileHandle,&findData ) ) PrintFindData( &findData ); } FindClose( fileHandle ); } // //----< test stub >-------------------------------------------- int main(int argc, char *argv[]) { cout << "\n Demonstrate Finding Directory Contents " << "\n ========================================\n\n"; if(argc == 1) ListDirectoryContents( "*.*" ); else ListDirectoryContents(argv[1]); cout << endl; return( 0 ); }