日志源代码,删除指定路径所有文件夹名,并创建新的日志文件

#include "stdafx.h"
#pragma warning( disable : 4786) // Disable warning messages
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "shlwapi.h"
#include "PComm.h"
#include "CommTools.h"
#include  
#include
#include
#include
using namespace std;

/*****************************************************************************
 * 函 数 名  : GetCurrentDirFileFolder
 * 负 责 人  : guoyoung 1110930
 * 创建日期  : 2016年10月21日
 * 函数功能  : 获取给定路径下的所有文件夹名
 * 输入参数  : string path            指定的路径   
 * 输出参数  : vector& files  当前路径下的文件夹名
 * 返 回 值  : int
*****************************************************************************/
static int GetCurrentDirFileFolder(string path, vector& files)
{
    long hFile = 0;
    struct _finddata_t fileinfo;
    string p;
    hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo);
    if (-1 != hFile)
    {
        do
        {
            if (fileinfo.attrib & _A_SUBDIR)
            {
                if ((0 != strcmp(fileinfo.name, "."))
                    && (0 != strcmp(fileinfo.name, "..")))
                {
                    files.push_back(fileinfo.name);
                }
            }
        }while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
    return 0;
}


//创建日志文件和删除N个月前的日志文件夹。
void CLogfile::CreateFileOrDeleteFile(char *LogFile, struct tm **tim, FILE **fp)
{
 long i=0;
 char SubDirName[MAX_NAME] = {0};
    char tmpStr[MAX_NAME] = {0};
 char logPath[MAX_PATH];
    char DateDirName[MAX_NAME] = {0};
    vector files;
    vector Dayfiles;
    int size = 0;
    unsigned int ThrdValue = 0;
    unsigned int TargetValue = 0;
    char YearMonthFileName[12] = {0};
    char YearMonthfilePath[MAX_NAME] = {0};
    char DayfilePath[MAX_NAME] = {0};
   
 memset(SubDirName, 0x0, sizeof(SubDirName));
 memset(tmpStr, 0x0, sizeof(tmpStr));
 memset(logPath, 0x0, sizeof(logPath));
 //strcpy(logPath, "C:\\GWI_log");
 strcpy(logPath, g_logRootDir);
 time(&i);
 *tim=localtime(&i);
 strcpy(SubDirName, logPath);
    if (!FolderExist(SubDirName))
    {
     CreateDirectory(SubDirName, NULL);
    }
   
 sprintf(SubDirName,"%s\\%04d%02d", logPath, (*tim)->tm_year+1900, (*tim)->tm_mon+1);
 if (!FolderExist(SubDirName)) //if (!PathFileExists(SubDirName))
 {
        //按照年月时间删除历史日志文件
        if((*tim)->tm_mon < g_loglastmonthnum)
  {
            sprintf(YearMonthFileName,"%04d%02d", (*tim)->tm_year+1900-1, (*tim)->tm_mon+1-g_loglastmonthnum+12);
  }
  else
  {
            sprintf(YearMonthFileName,"%04d%02d", (*tim)->tm_year+1900, (*tim)->tm_mon+1-g_loglastmonthnum);
  }
  //modify by guoyong 1110930 2016-09-19, when log file last month is 0, it means the log file is not removed permanently.
  if (0 != g_loglastmonthnum)
  {
            ThrdValue = strtoul(YearMonthFileName, '\0', 10);
           
            /* 获取日志路径下 C:\GWI_log\所有年月文件夹, 如文件夹201604 */
            GetCurrentDirFileFolder(logPath, files);
            size = files.size();
            for (int i = 0; i < size; i++)
            {
                const char *CurrentFileName = files[i].c_str();
                TargetValue = strtoul(CurrentFileName,'\0', 10);
                if ((TargetValue != 0)
                    && (TargetValue <= ThrdValue))
                {
                    strcpy(YearMonthfilePath, logPath);
                    strcat(YearMonthfilePath, "\\");
                    strcat(YearMonthfilePath, CurrentFileName);
                    strcpy(tmpStr, YearMonthfilePath);
                   
                    /* 获取日志路径下 C:\GWI_log\201604\所有日期文件夹 */
                    GetCurrentDirFileFolder(YearMonthfilePath, Dayfiles);
                    int iDaysize = Dayfiles.size();
                    for (int j = 0; j < iDaysize; j++)
                    {
                        const char *DayFileName = Dayfiles[j].c_str();
                        strcpy(DayfilePath, YearMonthfilePath);
                        strcat(DayfilePath, "\\");
                        strcat(DayfilePath, DayFileName);
                       
                        DeleteDirectory(DayfilePath);
                        memset(DayfilePath, 0x0, sizeof(DayfilePath));
                    }
                    DeleteDirectory(tmpStr);
                    memset(YearMonthfilePath, 0x0, sizeof(YearMonthfilePath));
                    memset(tmpStr, 0x0, sizeof(tmpStr));
                }  
            }  
  }
       
  CreateDirectory(SubDirName, NULL);
 }
    sprintf(DateDirName,"%s\\%04d%02d\\%2d", logPath, (*tim)->tm_year+1900, (*tim)->tm_mon+1, (*tim)->tm_mday);
    if (!FolderExist(DateDirName))
    {
        CreateDirectory(DateDirName, NULL);
    }
   
 memset(m_filename, 0x0, sizeof(m_filename));
 char *pp = strrchr(LogFile, '\\');
 if( pp )
 {
  sprintf(m_filename, "%s\\%s_%04d%02d%02d.log", DateDirName, pp+1, (*tim)->tm_year+1900, (*tim)->tm_mon+1, (*tim)->tm_mday);
 }
 else
 {
  sprintf(m_filename, "%s\\%s_%04d%02d%02d.log", DateDirName, LogFile, (*tim)->tm_year+1900, (*tim)->tm_mon+1, (*tim)->tm_mday);
 }
 *fp=fopen(m_filename, "at");
}

你可能感兴趣的:(C语言,C++,日志,获取路径下的所有文件夹)