英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论