最近のLOGレコードファイルを削除
1475 ワード
// Delete Expire Log Files
BOOL DeleteExpireFiles(LPCTSTR lpszFileExt, SYSTEMTIME* pSystemTime, int nReservedDays)
{
BOOL bRet = FALSE;
do
{
ASSERT(NULL != lpszFileExt);
ASSERT(NULL != pSystemTime);
if (nReservedDays <= 0) { break; }
TCHAR szDir[MAX_PATH] = { 0 };
GetModuleFileName(NULL, szDir, _countof(szDir) - 1);
ASSERT(_tcslen(szDir) > 0);
_tcsrchr(szDir, TEXT('\\'))[0] = TEXT('\0');
TCHAR szLog[MAX_PATH] = { 0 };
StringCchPrintf(szLog, _countof(szLog), TEXT("%s\\*%s"), szDir, lpszFileExt);
WIN32_FIND_DATA fd = { 0 };
HANDLE hFind = FindFirstFile(szLog, &fd);
if (INVALID_HANDLE_VALUE == hFind) { break; }
CTime current(*pSystemTime);
while (TRUE)
{
if (!(FILE_ATTRIBUTE_DIRECTORY & fd.dwFileAttributes))
{
CTime time = CTime(fd.ftCreationTime);
CTimeSpan span(nReservedDays, 0, 0, 0);
if ((current - time) > span)
{
TCHAR szFile[MAX_PATH] = { 0 };
StringCchPrintf(szFile, _countof(szFile), TEXT("%s\\%s"), szDir, fd.cFileName);
DeleteFile(szFile);
}
}
if (!FindNextFile(hFind, &fd)) { break; }
}
if (INVALID_HANDLE_VALUE != hFind) { FindClose(hFind); hFind = INVALID_HANDLE_VALUE; }
// Completed
bRet = TRUE;
} while (0);
return bRet;
}
// : e.g log , log
SYSTEMTIME st = { 0 };
GetSystemTime(&st);
DeleteExpireFiles(TEXT(".log"), &st, 3);