其实这个和solr没什么关系了,只不过写出来作为一个笔记
由于大量的solr测试工作在运行,会导致tomcat产生大量大量的log file(在 %tomcat%\logs中),最初的想法是定期去清理log目录下7天前的log文件。于是刚开始想使用bat实现,但是发现bat对于判断n天前这个实现起来对我来说有点困难。。。。又考虑到服务器上没有JDK和.net Framework,所以决定用C++来实现,同时只需要将几个.dll文件一并拷贝到服务器上即可运行。
#include <iostream>
#include <io.h>
#include <direct.h>
#include <string>
#include <vector>
#include <iomanip>
#include <time.h>
#include <assert.h>
using namespace std;
void getFiles(string path, vector<string>& files){
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("/*").c_str(),&fileinfo)) != -1) {
do{
if(strcmp(fileinfo.name,".")!=0 && strcmp(fileinfo.name,"..")!=0)
files.push_back( p.assign(path).append("\\").append(fileinfo.name) );
}while(_findnext(hFile, &fileinfo)==0);
_findclose(hFile);
}
}
string getTime(string str,string del_flag)
{
string tmp="";
int k=del_flag.length();
int total=str.length();
for(int i=10;i>0;i--)
{
tmp+=str[total-k-i];
}
return tmp;
}
int str_2_int(string str)
{
assert(str.size()>0);
int pos = 0;
int sym = 1;
if(str[pos] == '+')
pos++;
else if(str[pos] == '-')
{
pos++;
sym=-1;
}
int num =0;
while(pos<str.length())
{
assert(str[pos]>='0');
assert(str[pos]<='9');
num = num*10+(str[pos]-'0');
assert(num>=0);
pos++;
}
num*=sym;
return num;
}
//if judge() return true, then this file is created 7 days ago and it should be deleted
bool judge(int y,int m,int d,int diff=7)
{
time_t timer;
struct tm *tblock;
timer=time(&timer);
tblock=localtime(&timer);
//current time
int year=tblock->tm_year+1900;
int month=tblock->tm_mon+1;
int day=tblock->tm_mday;
if((year-y)*365+(month-m)*30+(day-d)>diff)
return true;
return false;
}
bool valid(string str, string def_flag,int day_num)
{
str = getTime(str,def_flag);
string yy="";
string mm="";
string dd="";
int flag=0;
for(int i=0;i<3;i++)
{
if(flag==0)
{
if(str[i]!='-')
yy+=str[i];
else{
flag++;
continue;
}
}
if(flag==1)
{
if(str[i]!='-')
mm+=str[i];
else{
flag++;
continue;
}
}
if(flag==2)
{
if(str[i]!='-')
dd+=str[i];
else{
flag++;
continue;
}
}
}
int fday=str_2_int(dd);
int fmonth=str_2_int(mm);
int fyear=str_2_int(yy);
if(judge(fyear,fmonth,fday,day_num))
return true;
return false;
}
//the first parameter is del_flag;
//the second parameter is the file directory
//the third parameter is the number of days
void main(int argc,char**argv){
vector<string> files;
string del_flag=argv[1];
string tmp="";
int m=0;
char cmd[80];
int total=0;
getFiles(argv[2], files );
for (int j=0; j<files.size();j++){
for(int i=del_flag.length(),m=files[j].length();i>0;i--)
{
tmp+=files[j][m-i];
}
if(tmp==del_flag)
{
if(valid(files[j],del_flag,str_2_int(argv[3])))
{
tmp="del \"" + files[j] +"\"";
system(tmp.c_str());
total++;
cout<<files[j]<<" deleted......done"<<endl;
}
}
tmp="";
}
cout<<total<<" "<<del_flag<<" files deleted..."<<endl;
}
写完这段代码以后不得不说,C++真心难写,麻烦.....
好了,到这里代码任务结束了,其中还涉及到了以前没有做过的一个东西,就是带参数运行的C++程序。
在void main(int argc,char**argv) 函数中,argc代表参数个数,但是其实在main运行时不需要搭理这个argc,它只是用来记录我们传给main的参数个数。而argv其实是一个字符串数组,argv[0]中的内容很有意思,记录着当前main这个程序所在的目录,接下来才是我们传给main的。
到这里,程序已经写好了,下一步就是如何定制计划去启动这个程序。windows下自带的task scheduler是神器,只需要将这个程序加入到task scheduler中,并且设置好默认参数以及运行的条件和事件等,便可以让它定时自动运行啦!