Inno Setup的:读取一个文件修改时间,并用它来设置文件的时间在整个目录(Inno Setup

2019-07-28 22:44发布

在我安装,我从没有存储时间/日期属性档案解压文件,因此当要提取他们的最后修改日期设置为当前日期。 我想将其设置为存档文件的最后修改日期,但我无法弄清楚如何。 我试着用从代码的片段在这里 ,并在这里 ,但同时也没有给出任何错误,它没有改变的时候工作。 最近更新日期将需要改变*。*的文件夹中。

而且,当我需要挂接到用户是否取消设置删除这些文件,并开始回退的变化? 我知道了在UninstallDelete照顾但如果用户取消安装。

编辑:忽略第二部分,我竟然想通了后不久,我张贴在这里。 加入我自己的清理()来DeinitializeSetup()与卸载注册表项的检查。

下面是代码我试图将它添加到的部分:

procedure VolExtract(VWorld: String);
var
  ResultCode: Integer;
  VPath: String;
begin
  // Files are extracted to {app}\VWorld\One, {app}\VWorld\Two, etc.
  VPath := ExpandConstant('{app}\' + VWorld);
  WizardForm.FilenameLabel.Caption := DriveLetter + VWorld + '\one.vol';
  if Exec(ExpandConstant('{tmp}\volextract.exe'), '"' + DriveLetter + VWorld + '\one.vol" "' + VPath + '"', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) and (ResultCode = 0) then
  begin
    // Yep, it executed successfully
    WizardForm.FilenameLabel.Caption := DriveLetter + VWorld + '\two.vol';
    if Exec(ExpandConstant('{tmp}\volextract.exe'), '"' + DriveLetter + VWorld + '\two.vol" "' + VPath + '"', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) and (ResultCode = 0) then
    begin
      // Next
      WizardForm.FilenameLabel.Caption := DriveLetter + VWorld + '\three.vol';
      if Exec(ExpandConstant('{tmp}\volextract.exe'), '"' + DriveLetter + VWorld + '\three.vol" "' + VPath + '"', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) and (ResultCode = 0) then
      begin
        // Next
        WizardForm.FilenameLabel.Caption := DriveLetter + VWorld + '\four.vol';
        Exec(ExpandConstant('{tmp}\volextract.exe'), '"' + DriveLetter + VWorld + '\four.vol" "' + VPath + '"', '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
      end;
    end;
  end;
  if ResultCode <> 0 then
  begin
    // Handle Fail
    CDFound := False;
    MsgBox(CustomMessage('FileErr'), mbInformation, MB_OK);
    WizardForm.Close;
  end;
end;

Answer 1:

要改变的最后修改时间(我们称之为LastWriteTime现在)的所有文件从一个指定的目录由LastWriteTime某文件,使用下面的代码,你必须提取文件后。 您可以按照commented version之前的版本这篇文章,但请注意,我有错误出现(混合时间参数和未使用的文件标志参数),但点保持。

另外请注意,这个代码是InnoSetup的ANSI版本。 如果你需要用这个Unicode版本,你应该定义CreateFile函数导入为CreateFileW而不是CreateFileA或使用所建议的伎俩kobik在这个post

[code]
const
  OPEN_EXISTING = 3;  
  FILE_SHARE_WRITE = 2;
  GENERIC_WRITE = $40000000;
  INVALID_HANDLE_VALUE = 4294967295;

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

function FileSetTime(const AFileName: string; const ACreationTime, 
  ALastAccessTime, ALastWriteTime: TFileTime): Boolean;
var
  FileHandle: THandle;
begin
  Result := False;
  FileHandle := CreateFile(AFileName, GENERIC_WRITE, FILE_SHARE_WRITE, 0,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if FileHandle <> INVALID_HANDLE_VALUE then
  try
    Result := SetFileTime(FileHandle, ACreationTime, ALastAccessTime, 
      ALastWriteTime);
  finally
    CloseHandle(FileHandle);
  end;
end; 

procedure ModifyLastWriteTime(const ASourceFile, ATargetFolder: string);
var
  FindRec: TFindRec;
  LastWriteTime: TFileTime;
begin
  if FindFirst(ASourceFile, FindRec) then
  begin
    LastWriteTime := FindRec.LastWriteTime;
    if FindFirst(ATargetFolder + '*.*', FindRec) then
    try
      repeat
        if (FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
          FileSetTime(ATargetFolder + FindRec.Name, FindRec.CreationTime, 
            FindRec.LastAccessTime, LastWriteTime);
      until
        not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
  end;
end;

和使用。 所述的第一个参数ModifyLastWriteTime过程是从该LastWriteTime取源文件的名称。 第二个参数是哪些文件会被源文件得到修改了他们的LastWriteTime值(别忘了在目标文件夹参数尾部的反斜杠)的目录:

ModifyLastWriteTime('c:\SourceFile.xxx', 'c:\TargetFolder\')


Answer 2:

关于第二个问题,您可以删除已被调用过程中被提取的文件

Procedure CancelButtonClick(CurPageID: Integer; Var Cancel, Confirm: Boolean);
Begin
End;

正如CHM,部分帕斯卡尔脚本解释:事件功能

关于第一个问题,我建议你使用,而不是从归档中提取Inno Setup的[文件]部分。 你也许可以将此压缩包解压到本地文件夹(所以从你的身边,在编译之前,并添加此本地文件夹的[文件],但我可能误解你对文件的修改日期势在必行。



文章来源: Inno Setup: Read modified time from one file and use it to set the time for files in a whole directory