删除历史日志的一个API
删除历史日志的一个API
bool DeleteOldFiles(const char* strFolder, const char* strPrefix, bool is_recursion, UINT32 ulMinDateTime, UINT32 ulMaxDateTime)
{
HANDLE hFind = NULL;
WIN32_FIND_DATAA findFileData;
std::string strFindFolder = strFolder;
//转换成大写的进行比较
std::string strUpperPrefix = str_toupper(std::string(strPrefix));
std::string strUpperFileName(MAX_PATH, '\0');
std::string strTempName(MAX_PATH, '\0');
if (strFindFolder[strFindFolder.length() - 1] == '\\') {
strFindFolder.append("*.*");
} else {
strFindFolder.append("\\*.*");
}
hFind = FindFirstFileA(strFindFolder.c_str(), &findFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return false;
}
do{
if ((findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) {
if (is_recursion){
if ((0 == strcmp(".", findFileData.cFileName)) || (0 == strcmp("..", findFileData.cFileName)))
{
continue;
}
sprintf_s(&strTempName[0], MAX_PATH - 1, "%s\\%s", strFolder, findFileData.cFileName);
DeleteOldFiles(strTempName.c_str(), strPrefix, is_recursion, ulMinDateTime, ulMaxDateTime);
}
} else {
//判断文件名是否相同的前缀
strUpperFileName = str_toupper(std::string(findFileData.cFileName));
//有相同的前缀
if (strcmp(strUpperFileName.c_str(), strUpperPrefix.c_str()) >=0 ) {
//取当前指定位置的数值,转换成整数
UINT32 ulCurrDateTime = atoi(&strUpperFileName[strUpperPrefix.length()]);
if ((ulCurrDateTime >= ulMinDateTime) && (ulCurrDateTime <= ulMaxDateTime)) {
//组合文件名
sprintf_s(&strTempName[0], MAX_PATH - 1, "%s\\%s", strFolder, findFileData.cFileName);
//删除文件
DeleteFileA(strTempName.c_str());
}
}
}
} while (FindNextFileA(hFind, &findFileData));
::FindClose(hFind);
hFind = NULL;
return true;
}