/////////////////////////////////////////////////////////////// // fileattr.cpp - extract attribute information from files // // // // 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 using namespace std; //----< display file attributes >------------------------------ void ShowAttributes(DWORD attributes) { if(attributes & FILE_ATTRIBUTE_ARCHIVE ) cout << " archive\n"; if(attributes & FILE_ATTRIBUTE_DIRECTORY) cout << " directory\n"; if(attributes & FILE_ATTRIBUTE_HIDDEN ) cout << " hidden\n"; if(attributes & FILE_ATTRIBUTE_NORMAL ) cout << " normal\n"; if(attributes & FILE_ATTRIBUTE_READONLY ) cout << " read only\n"; if(attributes & FILE_ATTRIBUTE_SYSTEM ) cout << " system\n"; if(attributes & FILE_ATTRIBUTE_TEMPORARY) cout << " temporary\n"; } //----< display a FILETIME value >----------------------------- void ShowTime(FILETIME t) // Dumps the t to stdout { FILETIME ft; SYSTEMTIME st; FileTimeToLocalFileTime(&t, &ft); FileTimeToSystemTime(&ft, &st); char *append; if(st.wHour >= 12) append = "PM"; else append = "AM"; if(st.wHour == 0) st.wHour += 12; long save = cout.flags(); cout.fill('0'); cout << setw(2) << st.wMonth << "/" << setw(2) << st.wDay << "/" << st.wYear << " " << setw(2) << st.wHour << ":" << setw(2) << st.wMinute << " " << append << endl; cout.flags(save); } // //----< extract file information >----------------------------- void main() { char filename[MAX_PATH]; HANDLE fileHandle; BOOL success; BY_HANDLE_FILE_INFORMATION info; cout << "\n Demonstrate File Information Extraction " << "\n =========================================\n\n"; cout << " Enter filename: "; cin >> filename; fileHandle = CreateFile( filename, GENERIC_READ, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0 ); if (fileHandle == INVALID_HANDLE_VALUE) { cout << " Error number " << GetLastError() << endl << endl; return; } else { success = GetFileInformationByHandle(fileHandle, &info); if (success) { ShowAttributes(info.dwFileAttributes); cout << " Last write time: "; ShowTime(info.ftLastWriteTime); cout << " Volume serial number: " << info.dwVolumeSerialNumber << endl; cout << " File size: " << info.nFileSizeLow << endl; cout << " Number of links: " << info.nNumberOfLinks << endl; cout << " High index: " << info.nFileIndexHigh << endl; cout << " Low index = " << info.nFileIndexLow << endl << endl; } } }