What you are experiencing is well-known behavior of the Windows file system. Take a look at the following from stack overflow Windows filesystem: Creation time of a file doesn't change when while is deleted and created again. And here's another interesting read on the same subject The apocryphal history of file system tunnelling
Incorrect file creation date
Hello
I'm trying to create files with C++ but the creation time displayed is incorrect.
Here's the code:
[CODE]
do
{
++item;
sprintf(pathDirFile,"%s/%s%03d.bmp",pathDir,file1,item); f1 = fopen(pathDirFile,"r");
sprintf(pathDirFile,"%s/%s%03d.txt",pathDir,file2,item); f2 = fopen(pathDirFile,"r");
if (f1) fclose(f1);
if (f2) fclose(f2);
} while (f1 != NULL || f2 != NULL);
sprintf(pathDirFile,"%s/%s%03d.bmp",pathDir,file1,item); f1 = fopen(pathDirFile,"wb");
sprintf(pathDirFile,"%s/%s%03d.txt",pathDir,file2,item); f2 = fopen(pathDirFile,"w");
fprintf(f2,...);
fwrite(buffer,1,sizeBuffer,f1);
fclose(f2);
fclose(f1);
[/CODE]
This code creates two files for me:
- ImageXXX.bmp
- ScalesXXX.txt
with XXX the following number of the files that exist in the directory.
Everything works fine, except when I delete the files (it happens that I delete all but Image001.bmp and Scales001.txt) and I restart; He creates two files for me (Image002.bmp and Scales002.txt), but the creation date of Image002.bmp is wrong because it is older than the creation time (e.g. 10:15) while the creation date of Scales002.txt is correct (e.g. 11:38). If I delete these two files and restart, I get the same result (e.g. 10:15 for Image002.bmp and 11:39 for Image002.txt). The other files (Image003.bmp and Scales003.txt) created in a row always have a correct time. But if I delete all these files (Image002.bmp, Scales002.txt, Images003.bmp and Scales003.txt) and restart, I still have the same thing (e.g. 10:15 for Image002.bmp and 11:40 for Image002.txt). There must be one minute between the time I delete the files and the time I launch the application.
I don't know how this is done and where this old time is memorized.
Does anyone know where this old time is memorized please?
Does anyone know how to get the correct creation time (the same for both files, i.e. the recent time) please?
3 additional answers
Sort by: Most helpful
-
Armen SARIAN 20 Reputation points
2024-08-11T11:53:15.43+00:00 Thanks grateful, I did this and it works.
#include <windows.h> // SetFileToCurrentTime - sets creation time to current system time // Return value - TRUE if successful, FALSE otherwise // hFile - must be a valid file handle void SetFileToCurrentTime(const char* pathDirFile) { HANDLE hFile; FILETIME ft; SYSTEMTIME st; size_t origsize = strlen(pathDirFile) + 1; const size_t newsize = mbstowcs(NULL,pathDirFile,origsize) + 1; wchar_t wPathDirFile[newsize]; mbstowcs(wPathDirFile,pathDirFile,origsize); hFile = CreateFile(wPathDirFile,FILE_WRITE_ATTRIBUTES,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if (GetLastError() == ERROR_FILE_NOT_FOUND) {printf("Cannot create creation time of the file because ERROR_FILE_NOT_FOUND.\n"); return;} GetSystemTime(&st); if (!SystemTimeToFileTime(&st,&ft)) {printf("Cannot create creation time of the file because SystemTimeToFileTime failed.\n"); return;} // Converts the current system time to file time format. if (!SetFileTime(hFile,&ft,&ft,&ft)) {printf("Cannot create creation time of the file because SetFileTime failed.\n"); return;} if (!CloseHandle(hFile)) {printf("Cannot create creation time of the file because CloseHandle failed.\n"); return;} } int main(int argc, char *argv[]) { SetFileToCurrentTime("E:/Images Qt/tata.txt"); }
-
Armen SARIAN 20 Reputation points
2024-08-11T13:57:10.4766667+00:00 Thank you a lot.
- I use some #define, but it doesn't work :
#include <tchar.h> //#define _UNICODE //#define _MBCS //#define _MB_MAP_DIRECT //#define _NO_INLINING
- How can I do about GetLastError() because they write it should be called just after CreateFile() please?
- I do want SetFileTime(hFile,&ft,&ft,&ft) to change the 3 times.
- The https://learn.microsoft.com/en-us/answers/questions/1856800/incorrect-file-creation-date?comment=answer-1639817&page=1#comment-1669382 code doesn't work.
Sorry for my English.
-
Armen SARIAN 20 Reputation points
2024-08-12T07:23:57.3366667+00:00 Hi, Thanks.
I've already tried to print character such as beta or gamma and it doesn't work. I work on Qt Creator (Windows 7, 64 bits) and I've tried to change "Encodage par défaut" UTF-8, but it should not be here that I have to change choice.
I had given up.
Do you know how I can do please ?