Изменение даты создания файла/каталога
download set_file_time.exe
// Изменение даты создания, даты изменения, даты доступа файла или директории
// параметры командной строки: путь к файлу + перечисление всех частей даты (локальное время)
// или просто путь к файлу и тогда устанавливается значение локального времени.
//cd "C:\Program Files\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin"
//x86_64-w64-mingw32-gcc.exe SetFileTime.c -o SetFileTime.exe
//cd "C:\Program Files (x86)\C-Free 5\mingw\bin"
//gcc.exe SetFileTime.c -o SetFileTime_32bit.exe
#include <windows.h>
#include <stdio.h>
int leapYear(int year);
int main(int argc, char * argv[]){
HANDLE fl;
SYSTEMTIME st;
FILETIME ft;
TIME_ZONE_INFORMATION tz;
DWORD dw, rezTZ;
int bias, temp, ow, fmounth, leap;
int SetNow = 0;
int dateParts[6], i;
unsigned short ndays[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, maxday;
if ( argc < 2 ) {
printf("Enter a file or dir path.");
return 0;
}else if ( argc == 2 ){
SetNow = 1;
}else if ( argc != 8 ){
printf("Need parameters format: SetFileTime.exe parPathNameFile [Year Month Day Hour Minute Second]");
return 0;
}
fl = CreateFile(argv[1], GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); //GENERIC_READ | GENERIC_WRITE
if (fl == INVALID_HANDLE_VALUE)
{
// Handle the error.
//printf ("CreateFile failed with error %d.\n", GetLastError());
MessageBox(NULL, "CreateFile failed with error.", "Error", MB_OK);
dw = GetLastError();
printf("Err CreateFile(): %ld", (long)dw);
return 1;
}
if ( !SetNow ) {
for (i = 2; i < 8; i++)
dateParts[i-2] = atoi(argv[i]);
st.wYear = dateParts[0];
st.wMonth = dateParts[1];
st.wDay = dateParts[2];
st.wHour = dateParts[3];
st.wMinute = dateParts[4];
st.wSecond = dateParts[5];
st.wMilliseconds = 666;
rezTZ = GetTimeZoneInformation(&tz);
/* rezTZ = TIME_ZONE_ID_UNKNOWN //russia UTC+03
tz.Bias = -180
tz.StandardBias = 0
tz.DaylightBias = -60
*/
if ( rezTZ == TIME_ZONE_ID_UNKNOWN || rezTZ == TIME_ZONE_ID_STANDARD )
bias = tz.Bias; // -180
else if ( rezTZ == TIME_ZONE_ID_DAYLIGHT )
bias = (int)tz.Bias + (int)tz.DaylightBias; // -180 + -60
else
bias = 0;
//bias = 210; -210 //tests
if ( bias != 0 ){
temp = st.wMinute;
temp += bias % 60;
if ( temp < 0 ){
st.wMinute = 60 + temp;
ow = -1;
} else if ( temp > 59 ){
st.wMinute = temp - 60;
ow = +1;
} else{
st.wMinute = temp;
ow = 0;
}
temp = st.wHour + ow + bias/60;
if ( temp < 0 ){
st.wHour = 24 + temp;
ow = -1;
}else if ( temp > 23 ){
st.wHour = temp - 24;
ow = +1;
} else {
st.wHour = temp;
ow = 0;
}
//DAY
if ( ow != 0 ) {
temp = (int)st.wDay + ow;
leap = leapYear(st.wYear);
maxday = ndays[st.wMonth -1];
if( st.wMonth == 2 ){
if ( leap )
maxday = 29;
else
maxday = 28;
}
if ( temp == 0 ){
fmounth = st.wMonth - 1;
if ( fmounth == 0 )
fmounth = 12;
maxday = ndays[fmounth - 1];
if( fmounth == 2 ){
if ( leap )
maxday = 29;
else
maxday = 28;
}
st.wDay = maxday;
ow = -1;
} else if ( temp > maxday ){
st.wDay = 1;
ow = +1;
} else {
st.wDay = temp;
ow = 0;
}
}
// MONTH
if ( ow != 0 ) {
temp = (int)st.wMonth + ow;
if ( temp == 0 ){
st.wMonth = 12;
ow = -1;
}else if ( temp == 13 ){
st.wMonth = 1;
ow = +1;
}else{
st.wMonth = temp;
ow = 0;
}
}
// YEAR
if ( ow != 0 ) {
st.wYear += ow;
}
//printf("%u %u %u %u %u %u\n", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
}
//printf("st.wHour = %u, st.wMinute = %u, bias/60 = %i\n", st.wHour, st.wMinute, bias/60);
/*FILE * file;
file = fopen("test.txt", "wb");
fwrite(&tz, sizeof(TIME_ZONE_INFORMATION), 1, file );
fclose(file);
printf("rezTZ = %u, tz.Bias = %i, tz.DaylightBias = %i\n", rezTZ, tz.Bias, tz.DaylightBias);
wprintf(L"%s\n", tz.StandardName);
*/
}else{
GetSystemTime(&st); // time in Coordinated Universal Time (UTC).
//GetLocalTime(&st); // time in local time - добавляет 3ч
}
if ( SystemTimeToFileTime(&st, &ft) == 0){
printf("Err SystemTimeToFileTime().");
CloseHandle(fl);
return 0;
}
if ( SetFileTime(fl, &ft, &ft, &ft) == 0 ){
dw = GetLastError();
printf("Err SetFileTime(): %ld", (long)dw);
}
CloseHandle(fl);
return 0;
}
// високосный год ?
int leapYear(int year){
if ( year % 4 == 0 ){
if ( year % 100 == 0 ){
if ( year % 400 == 0 )
return 1;
else
return 0;
}else
return 1;
}else
return 0;
}
to be continued...