在Inno Setup中以随机数作为参数执行程序?

时间:2018-05-21 10:31:42

标签: inno-setup pascalscript

如何在Inno Setup中的字符串中包含随机数?

[Run]
#define rndH Random(24) 
#define rndM Random(60)

Filename: "schtasks"; \
  Parameters: "/Create /F /SC DAILY /ST {rndH}:{rndM} /RU SYSTEM /RL HIGHEST /TN ""Program Title"" /TR ""'{app}\program.exe'""";

当我尝试上面的代码时,我收到错误

  

[ISPP]未声明的标识符:“随机”

由于

2 个答案:

答案 0 :(得分:0)

如果有人有这样的问题,我设法用下面的代码来做。

[Run]
Filename: "schtasks"; \
  Parameters: "/Create /F /SC DAILY /ST {code:MyRandH}:{code:MyRandM} /RU SYSTEM /RL HIGHEST /TN ""Program Title"" /TR ""'{app}\program.exe'""";

[Code]
var
  h: Integer;
  m: Integer;

function MyRandH(Param: String): String;
begin
  h := Random(24);
  if h < 10 then
    Result := '0' + IntToStr(h)
  else
    Result := IntToStr(h);
end;

function MyRandM(Param: String): String;
begin
  m := Random(60);
  if m < 10 then
    Result := '0' + IntToStr(m)
  else
    Result := IntToStr(m);
end;

答案 1 :(得分:0)

你的答案中的代码有效,但它不必要地复杂化:

  • 使用scripted constant参数,以避免创建两个仅由常量不同的函数;
  • 使用Format function将数字填充为两位数字;
  • 不需要使用全局变量(虽然我的版本实际上根本不需要任何变量)。
[Run]
Filename: "schtasks"; \
  Parameters: "/Create /F /SC DAILY /ST {code:MyRand|24}:{code:MyRand|60} ...";
[Code]

function MyRand(Param: string): string;
begin
  Result := Format('%.2d', [Random(StrToInt(Param))]);
end;