Dateiattribute (Erstellungsdatum, Letzte Änderung, Letzter Zugriff) ändern
-
Hallo,
ich möchte gerne das Erstellungsdatum, Datum der letzten Änderung und Datum des letzten Zugriffs einer Datei ändern.
In der Winapi habe ich dazu die Funktion SetFileTime gefunden. Leider funktioniert das ganze nicht so wie ich das möchte bzw. die funktion liefert mir immer FALSE zurück und ändert entsprechend auch keinen Zeitstempel der Datei.
Habe zum testen folgendes Codeschnipsel verwendet:
WIN32_FIND_DATA pwfd; HANDLE fHandle = FindFirstFile( "C:\\test1.xml", &pwfd ); WIN32_FIND_DATA pwfd2; HANDLE fHandle2 = FindFirstFile( "C:\\autoexec.bat", &pwfd2 ); bool result = SetFileTime( fHandle, // handle to the file &pwfd2.ftCreationTime, // time the file was created NULL, // time the file was last accessed NULL // time the file was last written );
Also ich will in diesem Beispiel das Datum der Datei 2 (autoexec.bat) für die Datei 2 (test1.xml) setzen, funktioniert aber leider nicht. Hat jemand eine Ahnung warum?
Gruß
Daniel
-
Naja, du solltest auch ein gültiges File-Handle übergeben! Also ein Handle, welches mittels "CreateFile" und FILE_WRITE_ATTRIBUTES erzeugt wurde...
-
Ja, klingt sinnvoll, hatte ich gar nicht bedacht das ich ja schreibend zugreifen muss...
Sieht dann so aus und funktioniert auch:
HANDLE hFile = CreateFile( "C:\\test1.xml", // create MYFILE.TXT GENERIC_WRITE, // open for writing 0, // do not share NULL, // no security OPEN_EXISTING, // overwrite existing FILE_ATTRIBUTE_NORMAL | // normal file FILE_FLAG_OVERLAPPED, // asynchronous I/O NULL); // no attr. template WIN32_FIND_DATA pwfd2; HANDLE fHandle2 = FindFirstFile( "C:\\autoexec.bat", &pwfd2 ); bool result = SetFileTime( hFile, // handle to the file &pwfd2.ftCreationTime, // time the file was created NULL, // time the file was last accessed NULL // time the file was last written );
Danke für die schnelle Antwort.
-
Hi teste mal statt GENERIC_WRITE - FILE_WRITE_ATTRIBUTES so steht es jedenfalls hier:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/setfiletime.asp
Zitat:
hFile
[in] Handle to the file for which to set the dates and times. The file handle must have been created with the FILE_WRITE_ATTRIBUTES access right.MFG c°h°
-
Ich habs mal eben implementiert intressiert mich auch :
void SetFileTimeXY(){ HANDLE hFile = CreateFile( "C:\\datei1.txt", // create MYFILE.TXT FILE_WRITE_ATTRIBUTES, // open for writing 0, // do not share NULL, // no security OPEN_EXISTING, // overwrite existing FILE_ATTRIBUTE_NORMAL | // normal file FILE_FLAG_OVERLAPPED, // asynchronous I/O NULL); // no attr. template WIN32_FIND_DATA pwfd2; HANDLE fHandle2 = FindFirstFile( "C:\\datei2.txt", &pwfd2 ); if(fHandle2 == NULL || hfile == NULL ) return; int result = SetFileTime( hFile, // handle to the file &pwfd2.ftCreationTime, // time the file was created &pwfd2.ftCreationTime, // time the file was last accessed &pwfd2.ftCreationTime // time the file was last written ); printf("%i",result); }
so funktioniert das ganze auch ... fast
die Erstellungszeit von datei1.txt wird zwar geändert aber leider nicht auf exakt die Erstellungszeit von Datei2.txt man kann auch nicht grade sagen das die beiden Daten stark korrellieren. Sie liegen jedenfall um 1 Tag 3 Std und 16 Minuten auseinander.
Woran das liegen kann .... kaMFG ch
-
Ach ...
das lag daran das dir /O:D nicht die filecreatetime anzeigt sondern die LastWriteTime hab die nun dem file2 zugewiesen und es klappt