20.2.2 Example: A File Directory Showing File Creation Times

The following code displays a verbose directory of files in the current directory:

void PrintData(PWIN32_FIND_DATA pffd)

{

SYSTEMTIME stCreate, stWrite;

if (pffd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

return; /* don't display directories */

/* convert creation and write times to system format */

FileTimeToSystemTime(&pffd->ftCreationTime, &stCreate);

FileTimeToSystemTime(&pffd->ftLastWriteTime, &stWrite);

/* display the creation and write times */

printf("%-12s %02d/%02d/%d %02d:%02d",

pffd->cFileName,

stCreate.wDay, stCreate.wMonth, stCreate.wYear,

stCreate.wHour, stCreate.wMinute);

printf(" %02d/%02d/%d %02d:%02d %lu\n",

stWrite.wDay, stWrite.wMonth, stWrite.wYear,

stWrite.wHour, stWrite.wMinute, pffd->nFileSizeLow);

}

void main()

{

HANDLE hSearch;

WIN32_FIND_DATA ffd;

/* find the first file or say there are none */

hSearch = FindFirstFile("*.*", &ffd);

if (hSearch == INVALID_HANDLE_VALUE)

puts("no files");

/* display the header and do files until we're done */

else {

printf("\nfile name created");

printf(" last written size\n\n");

PrintData(&ffd);

while (FindNextFile(hSearch, &ffd))

PrintData(&ffd);

}

}