user13686758
user13686758

Reputation:

How to catch the error message of bat file for deleting files in Inno Setup?

I am running a bat file, after the installation is done CurStep = ssDone to perform some deletion operation. if the files and folders are not found at the specified location. It is exiting silently. i want to show the message if the file does not exits or any other error occurs during deletion how can i catch the bat file errors in Inno Setup.

Batch file:

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

Code:

[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;

Upvotes: 3

Views: 250

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202108

In general, you should test the ErrorCode for a non zero exit code.

But Windows del command does not report errors with exit code unfortunately:
Batch file and DEL errorlevel 0 issue

If you wanted to capture the output, see:
How to get an output of an Exec'ed program in Inno Setup?


And it's not good solution anyway to delete files using a batch file, if you can do the same directly in the Inno Setup Pascal Script code.

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;

And if you do not need to do this in the ssDone step (why would you?), just use the [InstallDelete] section.

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

Upvotes: 2

Related Questions