64,651
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <afxdisp.h>
#include <winbase.h>
COleDateTime dt;
char opt[5];//"/WAC"
int ArgNum;
int offset=1;
char *s;
char path[MAX_PATH];
char pathlnk[MAX_PATH+4];//.lnk
char tmp[20];//YYYY-MM-DD hh:mm:ss
HANDLE hfile;
BY_HANDLE_FILE_INFORMATION bhfi;
FILETIME ft;
SYSTEMTIME st;
int isGet=0;
int main(int argc,char **argv) {
setlocale(LC_ALL,"chs");
strcpy(opt,"/W");
dt=COleDateTime::GetCurrentTime();
if (argc==1) {
HELP:
printf("File or directory Datetime(WAC:lastWrite|lastAccess|Creation)\n");
printf("Set to Now or YY-MM-DD [hh:mm:ss] or Get\n");
printf(" FD [/WAC] File_or_Dir_name [[[20]YY-]MM-DD[ hh:mm:ss]]|get\n");
return 1;
}
for (ArgNum=1;ArgNum<argc;ArgNum++) {
if (ArgNum==0+offset) {
s=argv[ArgNum];
if (0==strcmp(s,"/?")) goto HELP;
if ('/'==s[0]) {
strncpy(opt,s,4);opt[4]=0;
strupr(opt);
if (NULL==strchr(opt,'A')&&NULL==strchr(opt,'C')&&strlen(opt)<4) strcat(opt,"W");
offset=2;
} else {
strncpy(path,s,MAX_PATH-1);
path[MAX_PATH-1]=0;
}
}
if (ArgNum==1+offset) {
s=argv[ArgNum];
if (0==stricmp(s,"get")) isGet=1;
else {
if (!dt.ParseDateTime(s)) {
printf("Error: Invalid datetime format\n");
return 2;
}
}
}
if (ArgNum==2+offset) {
strncpy(tmp,argv[ArgNum-1],10);tmp[10]=0;//YYYY-MM-DD
strcat(tmp," ");
strncat(tmp,argv[ArgNum],8);//hh:mm:ss
if (!dt.ParseDateTime(tmp)) {
printf("Error: Invalid datetime format\n");
return 2;
} else break;//
}
}
if (isGet) hfile = CreateFile(path, GENERIC_READ , FILE_SHARE_READ , NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
else hfile = CreateFile(path, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE!=hfile) {
if (!GetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
CloseHandle(hfile);
printf("Error: GetFileTime!\n");
return 4;
}
if (strchr(opt,'W')) {
if (isGet) {
dt=COleDateTime::COleDateTime(bhfi.ftLastWriteTime);
printf("File Datetime(LastWrite ) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
dt.GetAsSystemTime(st);
SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftLastWriteTime);
if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
printf("File Datetime(LastWrite ) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
CloseHandle(hfile);
printf("Error: SetFileTime!\n");
return 5;
}
}
}
if (strchr(opt,'C')) {
if (isGet) {
dt=COleDateTime::COleDateTime(bhfi.ftCreationTime);
printf("File Datetime(Creation ) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
dt.GetAsSystemTime(st);
SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftCreationTime);
if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
printf("File Datetime(Creation ) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
CloseHandle(hfile);
printf("Error: SetFileTime!\n");
return 5;
}
}
}
if (strchr(opt,'A')) {
if (isGet) {
dt=COleDateTime::COleDateTime(bhfi.ftLastAccessTime);
printf("File Datetime(LastAccess) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
dt.GetAsSystemTime(st);
SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftLastAccessTime);
if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
printf("File Datetime(LastAccess) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
CloseHandle(hfile);
printf("Error: SetFileTime!\n");
return 5;
}
}
}
CloseHandle(hfile);
return 0;
}
strcpy(pathlnk,path);
strcat(pathlnk,".lnk");
if (isGet) hfile = CreateFile(pathlnk, GENERIC_READ , FILE_SHARE_READ , NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
else hfile = CreateFile(pathlnk, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE!=hfile) {
if (!GetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
CloseHandle(hfile);
printf("Error: GetFileTime!\n");
return 4;
}
if (strchr(opt,'W')) {
if (isGet) {
dt=COleDateTime::COleDateTime(bhfi.ftLastWriteTime);
printf("File.lnk Datetime(LastWrite ) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
dt.GetAsSystemTime(st);
SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftLastWriteTime);
if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
printf("File.lnk Datetime(LastWrite ) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
CloseHandle(hfile);
printf("Error: SetFileTime!\n");
return 5;
}
}
}
if (strchr(opt,'C')) {
if (isGet) {
dt=COleDateTime::COleDateTime(bhfi.ftCreationTime);
printf("File.lnk Datetime(Creation ) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
dt.GetAsSystemTime(st);
SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftCreationTime);
if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
printf("File.lnk Datetime(Creation ) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
CloseHandle(hfile);
printf("Error: SetFileTime!\n");
return 5;
}
}
}
if (strchr(opt,'A')) {
if (isGet) {
dt=COleDateTime::COleDateTime(bhfi.ftLastAccessTime);
printf("File.lnk Datetime(LastAccess) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
dt.GetAsSystemTime(st);
SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftLastAccessTime);
if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
printf("File.lnk Datetime(LastAccess) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
CloseHandle(hfile);
printf("Error: SetFileTime!\n");
return 5;
}
}
}
CloseHandle(hfile);
return 0;
}
if (isGet) hfile = CreateFile(path, GENERIC_READ , FILE_SHARE_READ , NULL,OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
else hfile = CreateFile(path, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (INVALID_HANDLE_VALUE!=hfile) {
if (!GetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
CloseHandle(hfile);
printf("Error: GetFileTime!\n");
return 4;
}
if (strchr(opt,'W')) {
if (isGet) {
dt=COleDateTime::COleDateTime(bhfi.ftLastWriteTime);
printf("Directory Datetime(LastWrite ) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
dt.GetAsSystemTime(st);
SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftLastWriteTime);
if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
printf("Directory Datetime(LastWrite ) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
CloseHandle(hfile);
printf("Error: SetFileTime!\n");
return 5;
}
}
}
if (strchr(opt,'C')) {
if (isGet) {
dt=COleDateTime::COleDateTime(bhfi.ftCreationTime);
printf("Directory Datetime(Creation ) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
dt.GetAsSystemTime(st);
SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftCreationTime);
if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
printf("Directory Datetime(Creation ) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
CloseHandle(hfile);
printf("Error: SetFileTime!\n");
return 5;
}
}
}
if (strchr(opt,'A')) {
if (isGet) {
dt=COleDateTime::COleDateTime(bhfi.ftLastAccessTime);
printf("Directory Datetime(LastAccess) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
dt.GetAsSystemTime(st);
SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftLastAccessTime);
if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
printf("Directory Datetime(LastAccess) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
} else {
CloseHandle(hfile);
printf("Error: SetFileTime!\n");
return 5;
}
}
}
CloseHandle(hfile);
return 0;
}
printf("Error: Can not find or open File or File.lnk or Dir [%s]\n",path);
return 3;
}