Ilya
Ilya

Reputation: 5613

How can I "touch" a file from within an InnoSetup script?

How can I "touch" a file, i.e. update its' last modified time to the current time, from within an InnoSetup (Pascal) script?

Upvotes: 3

Views: 1263

Answers (1)

Ilya
Ilya

Reputation: 5613

Here's the code snippet for the TouchFile function:

[Code]
function CreateFile(
    lpFileName             : String;
    dwDesiredAccess        : Cardinal;
    dwShareMode            : Cardinal;
    lpSecurityAttributes   : Cardinal;
    dwCreationDisposition  : Cardinal;
    dwFlagsAndAttributes   : Cardinal;
    hTemplateFile          : Integer
): THandle;
#ifdef UNICODE
 external '[email protected] stdcall';
#else
 external '[email protected] stdcall';
#endif

procedure GetSystemTimeAsFileTime(var lpSystemTimeAsFileTime: TFileTime);
 external '[email protected]';

function SetFileModifyTime(hFile:THandle; CreationTimeNil:Cardinal; LastAccessTimeNil:Cardinal; LastWriteTime:TFileTime): BOOL;
external '[email protected]';

function CloseHandle(hHandle: THandle): BOOL;
external '[email protected] stdcall';

function TouchFile(FileName: String): Boolean;
const
  { Win32 constants }
  GENERIC_WRITE        = $40000000;
  OPEN_EXISTING        = 3;
  INVALID_HANDLE_VALUE = -1;
var
  FileTime: TFileTime;
  FileHandle: THandle;
begin
  Result := False;
  FileHandle := CreateFile(FileName, GENERIC_WRITE, 0, 0, OPEN_EXISTING, $80, 0);
  if FileHandle <> INVALID_HANDLE_VALUE then
  try
    GetSystemTimeAsFileTime(FileTime);
    Result := SetFileModifyTime(FileHandle, 0, 0, FileTime);
  finally
    CloseHandle(FileHandle);
  end;      
end;

Upvotes: 5

Related Questions