65,211
社区成员
发帖
与我相关
我的任务
分享
#include <time.h>
#include <stdio.h>
#include <string.h>
void main()
{
char tmpbuf[128];
time_t ltime;
struct tm *today;
time( <ime );
today = localtime( <ime );
strftime( tmpbuf, 128,
"%Y/%m/%d %H:%M:%S\n", today );
printf( tmpbuf );
}
#include <windows.h>
#include <cstdio>
#include <string>
using namespace std;
const __int64 nano100SecInDay=(__int64)10000000*60*60*24;
ULARGE_INTEGER now;
int days = 14;
bool backup = false;
string dst_dir;
string backup_dir;
void make_sure_dir_exist(string & file)
{
int tag = 2;
while(true)
{
tag = file.find_first_of("\\",tag+1);
if(tag!=-1)
{
string tmp = file.substr(0,tag);
::CreateDirectory(tmp.c_str(),NULL);
}
else
{
break;
}
}
}
void xdel(const char *dir)
{
char tempFileFind[1024];
sprintf(tempFileFind,"%s\\*.*",dir);
WIN32_FIND_DATA ffd;
HANDLE hFind;
hFind = ::FindFirstFile(tempFileFind,&ffd);
if(hFind == INVALID_HANDLE_VALUE)
{
printf("can't find %s\n",dir);
return;
}
while(true)
{
//printf("find %s\n",ffd.cFileName);
FILETIME &ft = ffd.ftLastWriteTime;
ULARGE_INTEGER ui;
memcpy(&ui,&ft,sizeof(ui));
__int64 t = now.QuadPart-ui.QuadPart;
if(ffd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
if(strcmp(ffd.cFileName, ".") && strcmp(ffd.cFileName, ".."))
{
char temp[1024];
sprintf(temp,"%s\\%s",dir,ffd.cFileName);
xdel(temp);
if(t>=nano100SecInDay*days)
{
if(::RemoveDirectory(temp))
{
printf("del dir %s ok\n",temp);
}
else
{
printf("del dir %s failed\n",temp);
}
}
}
}
else
{
char temp[1024];
sprintf(temp,"%s\\%s",dir,ffd.cFileName);
if(t>=nano100SecInDay*days)
{
if(backup)
{
string backup_file = temp;
backup_file.replace(0,dst_dir.length(),backup_dir.c_str());
make_sure_dir_exist(backup_file);
if(::MoveFile(temp,backup_file.c_str()))
{
printf("backup file %s ok\n",temp);
}
else
{
printf("backup file %s failed\n",temp);
}
}
else
{
SetFileAttributes(temp,FILE_ATTRIBUTE_NORMAL);
if(::DeleteFile(temp))
{
printf("del file %s ok\n",temp);
}
else
{
printf("del file %s failed\n",temp);
}
}
}
}
if (!FindNextFile(hFind, &ffd))
{
break;
}
}
FindClose(hFind);
}
int main(int argc,char ** argv)
{
if(argc<2)
{
printf("usage: xdel directory /d= /m=\n");
printf("[optional] /d: config the expired days,default is 14 days\n");
printf("[optional] /m: config the backup directory\n");
printf("for example: xdel d:\\test /d=10 /m=d:\\backup");
return 1;
}
for(int i=1;i<argc;++i)
{
string tmp = argv[i];
if(tmp.find("/d=")!=-1)
{
int d = atoi(tmp.substr(3).c_str());
if(d!=0)
{
days =d;
}
}
else if(tmp.find("/m=")!=-1)
{
backup = true;
backup_dir = tmp.substr(3);
}
else
{
dst_dir = tmp;
}
}
//Get system time
SYSTEMTIME st;
::GetSystemTime(&st);
FILETIME f;
::SystemTimeToFileTime(&st,&f);
memcpy(&now,&f,sizeof(now));
xdel(argv[1]);
return 0;
}
#include <time.h>
#include <stdio.h>
void main( void )
{
char dbuffer [9];
char tbuffer [9];
_strdate( dbuffer );
printf( "The current date is %s \n", dbuffer );
_strtime( tbuffer );
printf( "The current time is %s \n", tbuffer );
}