2

I have Inno Setup script that works well but I need to incorporate a feature in it. What I want to achieve during any installis as follows:

  1. Find out if two files exist (Done)
  2. If they both exist, then proceed with installation with over writting them (Done)
  3. If both files are missing, then install/copy new files to destination folder (Done)
  4. If one exist and the other is missing, then Get the creation date of the existing file. Create install the missing file but set the creation date and time to be exactly the same as the one that is already existing (Need help on this)

I have searched the whole forum and the only answer that cam close to this was this one.

Any help would be very much appreciated please.

Thanks.

Community
  • 1
  • 1
mtn
  • 468
  • 1
  • 4
  • 7

1 Answers1

1

Here is a set of functions which can get and set file times:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
OutputDir=userdocs:Inno Setup Examples Output

[Code]
#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif

const
  OPEN_EXISTING = 3;  
  GENERIC_READ = $80000000;
  FILE_WRITE_ATTRIBUTES = $0100;
  INVALID_HANDLE_VALUE = 4294967295;

type
  TFileTimes = record
    CreationTime: TFileTime;
    LastWriteTime: TFileTime;
    LastAccessTime: TFileTime;
  end;

function CreateFile(lpFileName: string; dwDesiredAccess, dwShareMode,
  lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes: DWORD;
  hTemplateFile: THandle): THandle; 
  external 'CreateFile{#AW}@kernel32.dll stdcall';
function CloseHandle(hObject: THandle): BOOL; 
  external 'CloseHandle@kernel32.dll stdcall';
function GetFileTime(hFile: THandle; out lpCreationTime, lpLastAccessTime,
  lpLastWriteTime: TFileTime): BOOL;
  external 'GetFileTime@kernel32.dll stdcall';
function SetFileTime(hFile: THandle; const lpCreationTime, lpLastAccessTime, 
  lpLastWriteTime: TFileTime): BOOL; 
  external 'SetFileTime@kernel32.dll stdcall';

function TryGetFileTimes(const FileName: string; out FileTimes: TFileTimes): Boolean;
var
  FileHandle: THandle;
  CreationTime: TFileTime;
  LastWriteTime: TFileTime;
  LastAccessTime: TFileTime;
begin
  Result := False;

  FileHandle := CreateFile(FileName, GENERIC_READ, 0, 0, OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL, 0);
  if FileHandle <> INVALID_HANDLE_VALUE then
  try
    Result := Boolean(GetFileTime(FileHandle, CreationTime, LastAccessTime, 
      LastWriteTime));
    if Result then
    begin
      FileTimes.CreationTime := CreationTime;
      FileTimes.LastWriteTime := LastWriteTime;
      FileTimes.LastAccessTime := LastAccessTime;
    end;
  finally
    CloseHandle(FileHandle);
  end;
end;

function SetFileTimes(const FileName: string; const FileTimes: TFileTimes): Boolean;
var
  FileHandle: THandle;
begin
  Result := False;

  FileHandle := CreateFile(FileName, FILE_WRITE_ATTRIBUTES, 0, 0, OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL, 0);
  if FileHandle <> INVALID_HANDLE_VALUE then
  try
    Result := Boolean(SetFileTime(FileHandle, FileTimes.CreationTime,
      FileTimes.LastAccessTime, FileTimes.LastWriteTime));
  finally
    CloseHandle(FileHandle);
  end;
end;

And here is their possible usage:

var
  FileTimes: TFileTimes;
begin
  if TryGetFileTimes('C:\TheFile.xxx', FileTimes) then
  begin
    if FileCopy('C:\TheNewFile.xxx', 'C:\TheFile.xxx', False) then
      SetFileTimes('C:\TheFile.xxx', FileTimes);
  end;
end;
TLama
  • 75,147
  • 17
  • 214
  • 392
  • I just want say a BIG thank you for helping me on this. I haven't been available since I posted my question. Please can you explain the following to me? `if FileCopy('C:\TheNewFile.xxx', 'C:\TheFile.xxx', False) then SetFileTimes('C:\TheFile.xxx', FileTimes);` Are you trying to copy TheNewFile.xxx and save as TheFile.xxx? From the help file since C:\TheFile.xxx already exist, FileCopy will evaluate to False. But should we be setting the file time for C:\TheFile.xxx or C:\TheNewFile.xxx as indicated in `SetFileTimes('C:\TheFile.xxx', FileTimes);` Help me clarify the logic please. – mtn Nov 13 '13 at 11:54
  • The code should overwrite the `TheFile.xxx` file with `TheNewFile.xxx` and set to the `TheFile.xxx` file times that has before that overwriting. – TLama Nov 13 '13 at 12:02
  • Thanks. I get the logic now and I truly appreciate your help. – mtn Nov 13 '13 at 12:06