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.

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.

Edited 7 Years Ago by Ancient Dragon: n/a

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.

This question has already been answered. Start a new discussion instead.