如何缓存由Inno Setup脚本常量返回的(带时间戳的)值?

huangapple go评论56阅读模式
英文:

How to cache a (timestamped) value of returned by Inno Setup scripted constant?

问题

function InitializeSetup(): Boolean;
begin
  { Set the timestamped folder once at the beginning of the setup }
  SetGlobalVar('BackupFolder', 
    'D:\Onderhoud\Onderhoud 2023\SystemA-' + GetDateTimeString('yyyymmdd-hhnn', '-', ':'));
  Result := True;
end;

function InspectieOnderhoudDir(Param: String): String;
begin
  { Use the timestamped folder set at the beginning of the setup }
  Result := ExpandConstant('{global:BackupFolder}\PROGRAMNAME_Backup\');
end;
英文:

I'm using Inno Setup to create backups in a system.
I've created a function that allows me to generate a folder with a timestamp, but that timestamp keeps on changing every time I run a command during the "setup".
The first command I run will put the files in folder: D:\Onderhoud\Onderhoud 2023\SystemA-20231231-2350\
The second command (within that same setup) will put the files in folder: D:\Onderhoud\Onderhoud 2023\SystemA-20231231-2352\
The third command in: D:\Onderhoud\Onderhoud 2023\SystemA-20231231-2359\
etc. etc.
I would like to generate one single variable that will be used during the setup, so from the moment setup starts it'll put the files in D:\Onderhoud\Onderhoud 2023\SystemA-20231231-2350\

My function is very basic and looks like:

function InspectieOnderhoudDir(Param: String): String;
begin
  result :=
    'Onderhoud ' + GetDateTimeString('yyyy', '-', ':') +
    '\SystemA-' + GetDateTimeString('yyyymmdd-hhmm', '-', ':');
end;

The commands I run in the [Run] section are:

Filename: "robocopy.exe"; \
    Parameters: "/MIR C:\PROGRAMNAME\ ""{app}\{code:InspectieOnderhoudDir}\PROGRAMNAME_Backup\ """; \
    Flags: runhidden; Components: Backup

; BackupNetworkSettings (added 25-10-2022)
Filename: "{cmd}"; \
    Parameters: "/c ""mkdir -p ""{app}\{code:InspectieOnderhoudDir}\Netwerk Instellingen\ """; \
    Flags: runhidden; Components: Backup
Filename: "{cmd}"; \
    Parameters: "/c ""netsh.exe -c interface dump > ""{app}\{code:InspectieOnderhoudDir}\Netwerk Instellingen\INEX\netsh.txt """; \
    Components: Backup\BackupNetworkSetting

etc. etc.

What do I need to change so my setup will generate a timestamped folder that remains the same during that setup, but changes when I start a new setup?

答案1

得分: 1

一个快速的解决方案是仅在第一次调用时生成该值并将其缓存:

var
  OnderhoudDir: string;

function InspectieOnderhoudDir(Param: string): string;
begin
  if OnderhoudDir = '' then
  begin
    OnderhoudDir :=
      'Onderhoud ' + GetDateTimeString('yyyy', '-', ':') + '\SystemA-' +
      GetDateTimeString('yyyymmdd-hhmm', '-', ':');
    Log(Format('Generated folder: %s', [OnderhoudDir]));
  end;

  Result := OnderhoudDir;
end;

一个更确定性的方法是在 InitializeSetup 事件函数 中生成该值:

var
  OnderhoudDir: string;

function InitializeSetup(): Boolean;
begin
  OnderhoudDir :=
    'Onderhoud ' + GetDateTimeString('yyyy', '-', ':') + '\SystemA-' +
    GetDateTimeString('yyyymmdd-hhmm', '-', ':');
  Log(Format('Generated folder: %s', [OnderhoudDir]));
  Result := True;
end;

function InspectieOnderhoudDir(Param: string): string;
begin
  Result := OnderhoudDir;
end;
英文:

A quick solution is to generate the value on the first call only and cache it:

var
  OnderhoudDir: string;

function InspectieOnderhoudDir(Param: string): string;
begin
  if OnderhoudDir = '' then
  begin
    OnderhoudDir :=
      'Onderhoud ' + GetDateTimeString('yyyy', '-', ':') + '\SystemA-' +
      GetDateTimeString('yyyymmdd-hhmm', '-', ':');
    Log(Format('Generated folder: %s', [OnderhoudDir]));
  end;

  Result := OnderhoudDir;
end;

More deterministic approach is to generate the value in InitializeSetup event function:

var
  OnderhoudDir: string;

function InitializeSetup(): Boolean;
begin
  OnderhoudDir :=
    'Onderhoud ' + GetDateTimeString('yyyy', '-', ':') + '\SystemA-' +
    GetDateTimeString('yyyymmdd-hhmm', '-', ':');
  Log(Format('Generated folder: %s', [OnderhoudDir]));
  Result := True;
end;

function InspectieOnderhoudDir(Param: string): string;
begin
  Result := OnderhoudDir;
end;

huangapple
  • 本文由 发表于 2023年3月9日 18:19:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定