如何在Inno Setup中捕获bat文件的错误消息以删除文件?

问题描述

安装完成CurStep = ssDone后,我正在运行bat文件以执行某些删除操作。如果在指定位置找不到文件文件夹。它正在悄无声息地退出。如果文件没有退出或在删除过程中发生任何其他错误,我想显示此消息。如何在Inno Setup中捕获bat文件错误

批处理文件

del "C:\archives\pages\*.txt"

代码

[Code]

procedure CurStepChanged(CurStep: TSetupStep);
var
  ErrorCode: Integer;
begin
  if CurStep = ssDone then
  begin
  log('Test'+ ExpandConstant('{tmp}\deletefiles.bat'))
    Exec(ExpandConstant('{tmp}\deletefiles.bat'),'',SW_HIDE,ewWaitUntilTerminated,ErrorCode); 
    log('Done')
  end;
end;

解决方法

通常,您应该测试ErrorCode的退出代码是否为非零。

不幸的是,Windows del命令不会通过退出代码报告错误:
Batch file and DEL errorlevel 0 issue

如果要捕获输出,请参见:
How to get an output of an Exec'ed program in Inno Setup?


如果您可以直接在Inno Setup Pascal脚本代码中执行同样的操作,那么使用批处理文件删除文件也不是一个好方法。

procedure CurStepChanged(CurStep: TSetupStep);
var
  FindRec: TFindRec;
begin
  if CurStep = ssDone then
  begin
    if not FindFirst('C:\path\*.txt',FindRec) then
    begin
      Log('Not found any files');
    end
      else
    begin
      try
        repeat
          if DeleteFile('C:\path\' + FindRec.Name) then
          begin
            Log(Format('Deleted %s',[FindRec.Name]));
          end
            else
          begin
            MsgBox(Format('Cannot delete %s',[FindRec.Name]),mbError,MB_OK);
          end;
        until not FindNext(FindRec);
      finally
        FindClose(FindRec);
      end;
    end;
  end;
end;

如果您不需要在ssDone步骤(为什么?)中执行此操作,只需使用[InstallDelete] section

[InstallDelete]
Type: files; Name: "C:\path\*.txt"