C++刪除指定文件夾下N天及之前日記文件的辦法。本站提示廣大學習愛好者:(C++刪除指定文件夾下N天及之前日記文件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C++刪除指定文件夾下N天及之前日記文件的辦法正文
本文實例講述了C++刪除指定文件夾下N天及之前日記文件的辦法。分享給年夜家供年夜家參考。詳細以下:
// 功效:刪除nDays天及之前的日記文件
// @nDays: 0-不刪除日記,3-刪除3天及之前的日記(保存明天、昨天、前天的日記) ...
void CRecordLog::ClearLog(UINT nDays) // 刪除N天前的日記
{
if (nDays > 0)
{
WIN32_FIND_DATA FindFileData;
CString sAllFile = m_sLogFolder + "\\*.log";
HANDLE hFind = ::FindFirstFile(sAllFile, &FindFileData);
if(INVALID_HANDLE_VALUE == hFind) return;
while(TRUE)
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) // 碰到文件夾
{
}
else // 碰到文件
{
CString sFileName;
sFileName.Format("%s", FindFileData.cFileName);
if (sFileName.GetLength() == 14)
{
CString sFileFullPath = m_sLogFolder + "\\" + sFileName;// 文件全途徑
sFileName.WordStr("-", "");
__int64 nFileName = _atoi64(sFileName); // 獲得文件的日期,如:20101030
CTime tNowTime = CTime::GetCurrentTime();
tNowTime = tNowTime - CTimeSpan(nDays, 0, 0, 0); // 指向nDays天前的日期
__int64 nNowTime = _atoi64(tNowTime.Format("%Y%m%d"));
if (20000000 < nFileName && nFileName < nNowTime)
{
::DeleteFile(sFileFullPath);
}
}
}
if(!FindNextFile(hFind, &FindFileData))
break;
}
FindClose(hFind);
}
}
彌補:下面被刪除的日記文件的文件名格局為:2011-02-08.log
願望本文所述對年夜家的C++法式設計有所贊助。