Mega Code Archive

 
Categories / Delphi / Files
 

Playing with File Dates (Data Hiding)

Title: Playing with File Dates (Data Hiding) Question: In my previous artice #1382 "How to get File Created,Modified and Accessed Dates" I demonstrated how to retieve these dates from a filename. This article extends this by allowing one to set ALL or just ONE of these file dates as well. The CREATED Datetime is interesting. This date is nice in that windows does not change this date as it does with MODIFIED and ACCESSED and it is not usually displayed in Explorers Grid (MODIFIED is Explorer's default) Windows allows you to set the CREATED date to be GREATER than the MODIFIED or ACCESSED dates. You could use this date for a variety of uses such as to keep track of the HIGHEST system date to check whether the system clock has been set backwards. If you are worried about this being greater than the MODIFIED date you could just use the HOURS:MINS:SECS of the datetime to store hidden data and leave the DD/MM/YYYY alone. eg. To Set ALL DateTimes SetFileTimes('c:\test.txt',Now,Now+3,Now+5); eg. To Set Created Datetime only SetFileTimes('c:\test.txt',Now,ftCreated); eg. To Get All Datetimes var dtC,dtM,dtA : TDateTime; GetFileTimes('c:\test.txt',dtC,dtM,dtA); eg. To Get Modified Dateime only GetFileTimes('c:\test.txt',dtM,ftModified) We are always looking for "Hidey Holes" to store data for copy protection and security schemes, this is another bullet for your ammo belt! Answer: unit PlayDates; interface uses Windows, SysUtils, DateUtils; type // Get-Set File Times TFileTimes = (ftCreated,ftModified,ftAccessed); function GetFileTimes(const AFileName : string; out ACreated : TDateTime; out AModified : TDateTime; out AAccessed : TDateTime) : boolean; overload; function GetFileTimes(const AFileName : string; out AFileTime : TDateTime; AGetFileTime : TFileTimes) : boolean; overload; function SetFileTimes(const AFileName : string; ACreated : TDateTime; AModified : TDateTime; AAccessed : TDateTime) : boolean; overload; function SetFileTimes(const AFileName : string; AFileTime : TDateTime; ASetFileTime : TFileTimes) : boolean; overload; // ----------------------------------------------------------------------- implementation // ================================================================ // Return the three dates (Created,Modified,Accessed) // of a given filename. Returns FALSE if file cannot // be found or permissions denied. Results are returned // in TdateTime VAR parameters // Overloaded version is for get of single filetime only // (Created OR Modified OR Accessed) // // To get extended error information, call GetLastError. // ================================================================ // Get Filetime for ALL function GetFileTimes(const AFileName : string; out ACreated : TDateTime; out AModified : TDateTime; out AAccessed : TDateTime) : boolean; var FileHandle : integer; bResult : boolean; FTimeC,FTimeA,FTimeM : TFileTime; LTime : TFileTime; STime : TSystemTime; begin FileHandle := FileOpen(AFileName,fmShareDenyNone); ACreated := 0.0; AModified := 0.0; AAccessed := 0.0; if FileHandle bResult := false else begin bResult := GetFileTime(FileHandle,@FTimeC,@FTimeA,@FTimeM); FileClose(FileHandle); if bResult then begin // Created FileTimeToLocalFileTime(FTimeC,LTime); if FileTimeToSystemTime(LTime,STime) then ACreated := EncodeDateTime(STime.wYear,STime.wMonth,STime.wDay, STime.wHour,STime.wMinute,STime.wSecond, STime.wMilliSeconds); // Modified FileTimeToLocalFileTime(FTimeM,LTime); if FileTimeToSystemTime(LTime,STime) then AModified := EncodeDateTime(STime.wYear,STime.wMonth,STime.wDay, STime.wHour,STime.wMinute,STime.wSecond, STime.wMilliSeconds); // Accessed FileTimeToLocalFileTime(FTimeA,LTime); if FileTimeToSystemTime(LTime,STime) then AAccessed := EncodeDateTime(STime.wYear,STime.wMonth,STime.wDay, STime.wHour,STime.wMinute,STime.wSecond, STime.wMilliSeconds); end; end; Result := bResult; end; // Get Filetime for Created OR Modified OR Accessed function GetFileTimes(const AFileName : string; out AFileTime : TDateTime; AGetFileTime : TFileTimes) : boolean; overload; var dtCreated, dtModified,dtAccessed : TDateTime; bResult : boolean; begin if GetFileTimes(AFileName,dtCreated,dtModified,dtAccessed) then begin case AGetFileTime of ftCreated : AFileTime := dtCreated; ftModified : AFileTime := dtModified; ftAccessed : AFileTime := dtAccessed; end; bResult := true; end else begin AFileTime := 0.0; bResult := false; end; Result := bResult; end; // ===================================================== // Set FileTimes for (Created,Modified,Accessed) // Overloaded for single Filetime // (Created OR Modified OR Accessed)\ // Returns false if function fails. // To get extended error information, call GetLastError. // or just one status // ===================================================== // Set file times for ALL function SetFileTimes(const AFileName : string; ACreated : TDateTime; AModified : TDateTime; AAccessed : TDateTime) : boolean; var FileHandle : integer; bResult : boolean; FTimeC,FTimeA,FTimeM, LTime : TFileTime; STime : TSystemTime; begin FileHandle := FileOpen(AFileName,fmShareDenyWrite or fmOpenWrite); if FileHandle bResult := false else begin // Created DecodeDateTime(ACreated,STime.wYear,STime.wMonth,STime.wDay, STime.wHour,STime.wMinute,STime.wSecond, STime.wMilliSeconds); SystemTimeToFileTime(STime,LTime); LocalFileTimeToFileTime(LTime,FTimeC); // Modified DecodeDateTime(AModified,STime.wYear,STime.wMonth,STime.wDay, STime.wHour,STime.wMinute,STime.wSecond, STime.wMilliSeconds); SystemTimeToFileTime(STime,LTime); LocalFileTimeToFileTime(LTime,FTimeM); // Accessed DecodeDateTime(AAccessed,STime.wYear,STime.wMonth,STime.wDay, STime.wHour,STime.wMinute,STime.wSecond, STime.wMilliSeconds); SystemTimeToFileTime(STime,LTime); LocalFileTimeToFileTime(LTime,FTimeA); bResult := SetFileTime(FileHandle,@FTimeC,@FTimeA,@FTimeM); FileClose(FileHandle); end; Result := bResult; end; // Set Filetime for Created OR Modified OR Accessed function SetFileTimes(const AFileName : string; AFileTime : TDateTime; ASetFileTime : TFileTimes) : boolean; var FileHandle : integer; bResult : boolean; FTime,LTime : TFileTime; STime : TSystemTime; pCreated,pModified,pAccessed : PFileTime; begin pCreated := nil; pModified := nil; pAccessed := nil; FileHandle := FileOpen(AFileName,fmShareDenyWrite or fmOpenWrite); if FileHandle bResult := false else begin DecodeDateTime(AFileTime,STime.wYear,STime.wMonth,STime.wDay, STime.wHour,STime.wMinute,STime.wSecond, STime.wMilliSeconds); SystemTimeToFileTime(STime,LTime); LocalFileTimeToFileTime(LTime,FTime); case ASetFileTime of ftCreated : pCreated := @FTime; ftModified : pModified := @FTime; ftAccessed : pAccessed := @FTime; end; bResult := SetFileTime(FileHandle,pCreated,pModified,pAccessed); FileClose(FileHandle); end; Result := bResult; end; end.