'로그삭제'에 해당되는 글 1건

  1. 일정 날짜 기준 로그파일 제거 1


본 코드는 30일이 지난 로그파일을 삭제합니다.

강조된 부분의 코드수정을 통해 제거 일정 기준을 변경시킬 수 있습니다.

  
//
// 폴더 경로 얻음
//
CString GetFolderPath(CString path)
{
	if(path.Right(1) == _T('\\'))
	{
		return path;
	}
	else
	{
		CString strFolderPath;
		strFolderPath = path.Left(path.ReverseFind(_T('\\'))+1);
		return strFolderPath;
	}
}

//
// 일정 날짜 기준 로그파일 제거
//
void DeleteLogfiles()
{
	// 로그파일 형태 20111116.log
	// 30일 기준 로그파일 삭제
	CTime CurTime = CTime::GetCurrentTime();
	CTime Day30Time;
	Day30Time = CurTime - CTimeSpan(30, 0, 0, 0); // 일, 시, 분, 초
	CString path, file_path, file_name;
	path.Format(_T("%s*.*"), _("로그디렉토리경로\\"));
	CFileFind finder;
	BOOL bRes;
	bRes = finder.FindFile(path);
	while(bRes)
	{
		bRes = finder.FindNextFile();
		if(!finder.IsDirectory()) // 폴더가 아니고 파일일 경우
		{
			// 삭제 상태 변수 초기화
			bool bDelete = false;
			// 현재 정보가 파일인 경우, file_data.cFileName에 파일이름이 들어있다.
			file_name = finder.GetFileName();
			file_path = GetFolderPath(path) + file_name;
			CString strLogDate;
			strLogDate = file_name.Left(8);
			// 문자 길이가 맞고, 숫자로만 구성되었는지 확인
			if(strLogDate.GetLength() == 8 && IsStringDigit(strLogDate))
			{
				int nLogYear = _ttoi(strLogDate.Left(4));
				int nLogMonth = _ttoi(strLogDate.Mid(4, 2));
				int nLogDay = _ttoi(strLogDate.Right(2));
				CTime LogTime(nLogYear, nLogMonth, nLogDay, 0, 0, 0, 0);
				if(LogTime < Day30Time)
					bDelete = true;
			}
			else
			{
				// 예외사항
				bDelete = true;
			}    
			if(bDelete)
			{
				// 30일이 지난 로그파일은 삭제
				DeleteFile(file_path);
			}
		}
	}
}