如何检查文件是否存在并要求在Pascal脚本代码中覆盖

问题描述 投票:2回答:1
function CreateLICStopServiceBatch(): boolean;
  begin
    Result := true;
    fileName := ExpandConstant('{code:GetBasicDirectoryValue}\{code:GetInstance}\_Service\Stop_LIC_Service.bat');
    SetArrayLength(lines, 3);
    lines[0] := ExpandConstant('set wrapdir={code:GetBasicDirectoryValue}\{code:GetInstance}\_Service');
    lines[1] := 'cd /d %wrapdir%';
    lines[2] := ExpandConstant('commons-daemon\prunsrv.exe //SS//{code:GetLSDVBServiceNameTEdit}');
    Result := SaveStringsToFile(filename,lines,false);
    exit;
  end;

CreateLICStopServiceBatch();

现在我只是将文件写入目录,无论文件是否存在都无关紧要。但我想检查文件是否存在(FileExists)并且还有机会选择(在对话框中)覆盖或获取已存在的旧文件。

inno-setup pascalscript
1个回答
1
投票
function CreateLICStopServiceBatch(): boolean;
var
  FileName: string;
  Lines: TArrayOfString;
begin
  FileName := GetBasicDirectoryValue + '\' + GetInstance + '\_Service\Stop_LIC_Service.bat';
  SetArrayLength(Lines, 3);
  Lines[0] := 'set wrapdir=' + GetBasicDirectoryValue + '\' + GetInstance + '\_Service';
  Lines[1] := 'cd /d %wrapdir%';
  Lines[2] := 'commons-daemon\prunsrv.exe //SS//' + GetLSDVBServiceNameTEdit;

  if (not FileExists(FileName)) or
     (MsgBox('Overwrite?', mbConfirmation, MB_YESNO) = idYes) then
  begin
    Result := SaveStringsToFile(FileName, Lines, False);
  end
    else
  begin
    { Not sure what you want to return when user does not confirm overwrite }
    Result := False;
  end;
end;
© www.soinside.com 2019 - 2024. All rights reserved.