/////////////////////////////////////////////////////////////// // filetime.cpp - extract time 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 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 time information from files >------------- void main() { HANDLE fileHandle; char filename[MAX_PATH]; FILETIME create, lastWrite, lastAccess; BOOL success; cout << "\n Demonstrate File Time Extraction " << "\n ==================================\n\n"; cout << " Enter filename: "; cin >> filename; // open file fileHandle = CreateFile( filename, GENERIC_READ, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0 ); if (fileHandle == INVALID_HANDLE_VALUE) { cout << " Error number " << GetLastError() << endl; return; } else { // extract file times success = GetFileTime( fileHandle, &create, &lastAccess, &lastWrite); cout << " Last write time: "; ShowTime(lastWrite); // the other two won't work in FAT systems } CloseHandle(fileHandle); cout << endl; }