Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding Files by a date

Status
Not open for further replies.

riggd

Technical User
Apr 8, 2001
3
0
0
AU
As I am a beginner with CB3 I am having a few difficulties... I was using the functions "FindFirstFile" and "FindNextFile" to search a subdirectory and locate files with the extension txt, which I managed to work ok.

Now I would like to search the subdirectory and locate files with a particular date. I am having trouble doing this. I tried to use FileTime but I don't ubderstand it..

Can anyone show me with simple code how to search for files with say the current date of the pc for example???
 
I'm not familar with FileTime. You might want to look at FileGetDate and FileAge. These return the last date the file was modified. The difference between the two is FileGetDate takes a file handle (which means the file must be open) and FileAge takes a file name.
Code:
int FileGetDate(int Handle);
int FileAge(const AnsiString FileName);

Both return an integer. (I suspect that FileTime may return something similar.) You can use this integer with FileDateToDateTime to get the modified date and time.
Code:
System::TDateTime FileDateToDateTime(int FileDate);

If you want to go further, you can take the TDateTime and turn it into a string so you can read it into an edit component. There are two functions to look at. The first, TimeToStr converts the TDateTime into a string with the time. The second, DateToStr converts the TDateTime into a string with the date.
Code:
AnsiString TimeToStr(TDateTime Time);
AnsiString DateToStr(TDateTime Date);

Putting this all together, you may have something like:
Code:
String FileName = "D:\\Temp\\TheFile.txt"; // The file's name
int IntFileDate = FileAge(FileName); // The date/time in integer format
TDateTime MyFileDateTime = FileDateToDateTime(IntFileDate); // The Date/Time
String MyFileTime = TimeToStr(MyFileDateTime); // The file's time
String MyFileDate = DateToStr(MyFileDateTime); // The file's date
[code]

As usual, this is "off the cuff" so I can't say if this will work exactly the way I think it will. One thing you should do, it put any convertions into a [b]try...catch[/b] block to catch any errors produced during the conversion.
[code]
try
{
     TDateTime MyFileDateTime = FileDateToDateTime(IntFileDate); // The Date/Time
}
catch ( ... )
{
     // Error code goes here
}

Hope this helps.

James P. Cottingham
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top