The date and time stamp of the file is stored in the find_t.wr_date and find_t.wr_time structure members. The file date is stored in a two-byte unsigned integer as shown here:
Because DOS stores a file’s seconds in two-second intervals, only the values 0 to 29 are needed. You simply multiply the value by 2 to get the file’s true seconds value. Also, because DOS came into existence in 1980, no files can have a time stamp prior to that year. Therefore, you must add the value “1980” to get the file’s true year value.
The following example program shows how you can get a directory listing along with each file’s date and time stamp:
#include <stdio.h>
#include <direct.h>
#include <dos.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
typedef struct find_t FILE_BLOCK;
void main(void);
void main(void)
{
FILE_BLOCK f_block; /* Define the find_t structure variable */
int ret_code; /* Define a variable to store return codes */
int hour; /* We’re going to use a 12-hour clock! */
char* am_pm; /* Used to print “am” or “pm” */
printf(“\nDirectory listing of all files in this directory:\n\n”);
/* Use the “*.*” file mask and the 0xFF attribute mask to list all files in the directory, including system files, hidden files, and subdirectory names. */
ret_code = _dos_findfirst(“*.*”, 0xFF, &f_block);
/* The _dos_findfirst() function returns a 0 when it is successful and has found a valid filename in the directory. */
while (ret_code == 0)
{
/* Convert from a 24-hour format to a 12-hour format. */
hour = (f_block.wr_time >> 11);
if (hour > 12)
{
hour = hour - 12;
am_pm = “pm”;
}
else
am_pm = “am”;
/* Print the file’s name, date stamp, and time stamp. */
printf(“%-12s %02d/%02d/%4d %02d:%02d:%02d %s\n”,
f_block.name, /* name */
(f_block.wr_date >> 5) & 0x0F, /* month */
(f_block.wr_date) & 0x1F, /* day */
(f_block.wr_date >> 9) + 1980, /* year */
hour, /* hour */
(f_block.wr_time >> 5) & 0x3F, /* minute*/
(f_block.wr_time & 0x1F) * 2, /* seconds */
am_pm);
/* Use the _dos_findnext() function to look for the next file in the directory. */
ret_code = _dos_findnext(&f_block);
}
printf(“\nEnd of directory listing.\n”);
}
Notice that a lot of bit-shifting and bit-manipulating had to be done to get the elements of the time variable and the elements of the date variable. If you happen to suffer from bitshiftophobia (fear of shifting bits), you can optionally code the preceding example by forming a union between the find_t structure and your own user-defined structure, such as this:
/* This is the find_t structure as defined by ANSI C. */
struct find_t
{
char reserved[21];
char attrib;
unsigned wr_time;
unsigned wr_date;
long size;
char name[13];
}
/* This is a custom find_t structure where we separate out the bits used for date and time. */
struct my_find_t
{
char reserved[21];
char attrib;
unsigned seconds:5;
unsigned minutes:6;
unsigned hours:5;
unsigned day:5;
unsigned month:4;
unsigned year:7;
long size;
char name[13];
}
/* Now, create a union between these two structures so that we can more easily access the elements of wr_date and wr_time. */
union file_info
{
struct find_t ft;
struct my_find_t mft;
}
Using the preceding technique, instead of using bit-shifting and bit-manipulating, you can now extract date and time elements like this:
...
file_info my_file;
...
printf(“%-12s %02d/%02d/%4d %02d:%02d:%02d %s\n”,
my_file.mft.name, /* name */
my_file.mft.month, /* month */
my_file.mft.day, /* day */
(my_file.mft.year + 1980), /* year */
my_file.mft.hours, /* hour */
my_file.mft.minutes, /* minute */
(my_file.mft.seconds * 2), /* seconds */
am_pm);
No comments:
Post a Comment