zoukankan      html  css  js  c++  java
  • 删除历史日志的一个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, '');
        std::string strTempName(MAX_PATH, '');
    
        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;
    }
  • 相关阅读:
    电影观后感
    自定义内存管理
    web.xml配置详解
    Annotation
    Centos中yum方式安装java
    linux下添加用户并赋予root权限
    Injector
    Container
    GlassFish的安装与使用(Windows)
    Version Control
  • 原文地址:https://www.cnblogs.com/eaglexmw/p/11290215.html
Copyright © 2011-2022 走看看