I have a com component that logs error details. I wan't to delete the log files that were created one month back. Where this code has to be written ? I mean it should be run only once when the COM compponent is loaded for the first time.

Recommended Answers

All 4 Replies

First you have to get a list of all the files in the folder ( see FindFirst() and FindNext() win32 api functions). Then for each of those call _stat() to get the file creation date and compare that with today's date. Finally, use remove() to delete the file.

Where should it be written: In the com component's startup code, probably in main(). If the com program is a windows service then you might want to put the code in another thread so that it can be put to sleep for 24 hours before running it again. This would assume the com component will stay in memory for a long long time, i.e. several days.

The com component is referenced in .net winforms application.

BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)

Is this the function were i need to write this code ?

Thanks for your reply. Find first and find next was usefull.

The com component is referenced in .net winforms application.

BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)

Is this the function were i need to write this code ?
How to compare file creation date and current system date.

First you have to get a list of all the files in the folder ( see FindFirst() and FindNext() win32 api functions). Then for each of those call _stat() to get the file creation date and compare that with today's date. Finally, use remove() to delete the file.

Where should it be written: In the com component's startup code, probably in main(). If the com program is a windows service then you might want to put the code in another thread so that it can be put to sleep for 24 hours before running it again. This would assume the com component will stay in memory for a long long time, i.e. several days.

>> How to compare file creation date and current system date

I would probably first call GetSystemTimeAsFileTime() to get the current system time into a FILETIME structure. Then just compare the two FILETIME structs.

It is not recommended that you add and subtract values from the FILETIME structure to obtain relative times. Instead, you should copy the low- and high-order parts of the file time to a ULARGE_INTEGER structure, perform 64-bit arithmetic on the QuadPart member, and copy the LowPart and HighPart members into the FILETIME structure.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.