我该如何“触摸" InnoSetup脚本中的文件? [英] How can I "touch" a file from within an InnoSetup script?

查看:56
本文介绍了我该如何“触摸" InnoSetup脚本中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从InnoSetup(Pascal)脚本中触摸"文件,即将文件的上次修改时间更新为当前时间?

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

推荐答案

以下是TouchFile函数的代码段:

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 'CreateFileW@kernel32.dll stdcall';
#else
 external 'CreateFileA@kernel32.dll stdcall';
#endif

procedure GetSystemTimeAsFileTime(var lpSystemTimeAsFileTime: TFileTime);
 external 'GetSystemTimeAsFileTime@kernel32.dll';

function SetFileModifyTime(hFile:THandle; CreationTimeNil:Cardinal; LastAccessTimeNil:Cardinal; LastWriteTime:TFileTime): BOOL;
external 'SetFileTime@kernel32.dll';

function CloseHandle(hHandle: THandle): BOOL;
external 'CloseHandle@kernel32.dll 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;

这篇关于我该如何“触摸" InnoSetup脚本中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆