一个定时备份log目录的程序

#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )//不显示控制台
#include "stdafx.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
//#include <Windows.h>
//#include <atlconv.h>
#include <afx.h>
#include <atlstr.h>
int main()
{
    printf("启动log自动备份程序.......");
    Sleep(3000);
    FreeConsole();//隐藏黑窗口
    while (true)
    {
        //当前时间是几点
        time_t tt = time(NULL);
        tm* t = localtime(&tt);
        int hour = t->tm_hour;
        if (hour == 8)// 每天8点时做一次备份
        {
            // 计算昨天的年月日
            time_t ltime;
            long MilSecond;
            struct   tm   tmcur;
            time(&ltime);
            MilSecond = ltime;
            MilSecond = MilSecond - 60 * 60 * 24;
            ltime = MilSecond;
            tmcur = *localtime(&ltime);
            int year = tmcur.tm_year + 1900; 
            int mon = tmcur.tm_mon + 1; 
            int day = tmcur.tm_mday;

            // 从ini配置文件里读取log路径和服务器路径
            LPCTSTR lpAppName = L"config";
            LPCTSTR lpKeyName = L"LogPath";
            LPCTSTR lpDefault = L"";
            wchar_t lpFileName[256];
            wchar_t logpath[256];
            wchar_t serverpath[256];

            TCHAR FPath[128];
            ::GetCurrentDirectory(MAX_PATH, FPath);
            swprintf(lpFileName, L"%ws\\config.ini", FPath);
            GetPrivateProfileString(lpAppName, lpKeyName, L"", LPWSTR(logpath), 256, lpFileName);
            lpKeyName = L"ServerPath";
            GetPrivateProfileString(lpAppName, lpKeyName, L"", LPWSTR(serverpath), 256, lpFileName);

            //直线拷贝命令
            char cmd[512];
            sprintf(cmd, "xcopy %ws\\%d%d%d %ws\\%d%d%d\\ /s/h/c/y/e/d", logpath, year, mon, day, serverpath, year, mon, day);
            system(cmd);
        }

        Sleep(60 * 60 * 1000); //休眠一个小时
    }
    
    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值