I want to create a batch file and run it, after the installation is done. I found a tutorial here: https://web.archive.org/web/20240227180634/https://www.codeproject.com/Questions/477984/createplusandpluswriteplusbatchplusfileplusinplusi
So I started to type this bunch of code here into my [Code]
section:
function CreateInstallDatabaseBatch(): boolean;
var
fileName: string;
line: string;
begin
Result:= True;
fileName:= ExpandConstant('{sd}\{#MyAppName}\Temp\installdatabase.bat');
line:= 'sqlcmd -S ' + srvName + ' -i {sd}\{#MyAppName}\Temp\installdatabase.sql';
Result:= SaveStringToFile(fileName, line, true);
exit;
end;
procedure installdb(CurStep: TSetupStep);
begin
if CurStep=ssDone then
begin
CreateInstallDatabaseBatch();
end;
end;
It compiles without any error and install my package without any errors, but it doesn't create the batch file with that one line. Do I have to add something? I just added the code parts that are showed in the tutorial and edited them a bit (i.e. I just need one line, not lines).
The CurStepChanged
from the original code is an Inno Setup event function. You cannot rename it.
Make sure the {sd}\{#MyAppName}\Temp
exists. Why don't you use an installer temporary folder ({tmp}
) instead?
You have to use the ExpandConstant
function to expand the {sd}
constant in the {sd}\{#MyAppName}\Temp\installdatabase.sql
(just as with the path to the batch file). You should better wrap the path to double-quotes too.
For a single command, you do not need to create a batch file. Just run the command directly using the ShellExec
function:
procedure CurStepChanged(CurStep: TSetupStep);
var
Params: string;
ErrorCode: Integer;
begin
if CurStep = ssDone then
begin
Params :=
'-S ' + srvName + ' ' +
'-i "' + ExpandConstant('{sd}\{#MyAppName}\Temp\installdatabase.sql') + '"';
ShellExec('', 'sqlcmd', Params, '', SW_SHOW, ewWaitUntilTerminated, ErrorCode);
end;
end;