#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(<ime);
MilSecond = ltime;
MilSecond = MilSecond - 60 * 60 * 24;
ltime = MilSecond;
tmcur = *localtime(<ime);
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;
}