Reputation: 111
Objective is to insert multiple lines (once) at beginning of text document.
But I have been having problems with the approaches I found. I've attempted to adjust them but it incorporates side-effect issues.
Two problems:
In reference to the other scripts I found both lines incorporating Result :=
resulted in unknown identifier.
References:
[Code]
procedure CurStepChanged(CurStep: TSetupStep);
var
ErrorCode: Integer;
FileName: string;
lines : TArrayOfString;
begin
fileName := ExpandConstant('{userappdata}\xy');
fileName := AddBackslash(FileName) + 'zlt.net';
SetArrayLength(lines, 6);
lines[0] := 'A';
lines[1] := 'B';
lines[2] := 'C';
lines[3] := 'D';
lines[4] := 'E';
lines[5] := 'F';
SaveStringsToFile(filename,lines,true);
end;
Upvotes: 4
Views: 214
Reputation: 202721
There's no way to insert lines into file. You have to recreate whole file.
CurStepChanged
is triggered for every step of the installation. You will want to insert the lines in one of the steps only (likely ssInstall
or ssPostInstall
)
procedure CurStepChanged(CurStep: TSetupStep);
var
Lines: TStringList;
begin
if CurStep = ssPostInstall then
begin
Lines := TStringList.Create;
try
Lines.LoadFromFile(FileName);
Lines.Insert(0, 'A');
Lines.Insert(1, 'B');
Lines.Insert(2, 'C');
Lines.Insert(3, 'D');
Lines.Insert(4, 'E');
Lines.Insert(5, 'F');
Lines.SaveToFile(FileName);
finally
Lines.Free;
end;
end;
end;
The code loads whole file to memory. That can be a problem if the file is too large. But that should not be a problem for files smaller than tens of MBs. Also repeatedly calling Insert
is not efficient. But for small files and few lines, this should not be a problem either.
Upvotes: 4