gpt4 book ai didi

batch-file - 如何在Inno Setup中捕获bat文件删除文件的错误信息?

转载 作者:行者123 更新时间:2023-12-04 15:15:09 24 4
gpt4 key购买 nike

我正在运行一个bat文件,安装完成后CurStep = ssDone执行一些删除操作。如果在指定位置找不到文件和文件夹。它悄无声息地退出。如果文件没有退出或在删除过程中发生任何其他错误,我想显示消息我如何在 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"

关于batch-file - 如何在Inno Setup中捕获bat文件删除文件的错误信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64477109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com