program NewScript;
var
List : TStringList;
begin
List := TStringList.Create;
List.SetString(0,'line1');
List.SetString(1,'line2');
List.SaveToFile('test.txt');
end.
I want the test.txt file to contain "line1" on the first line and "line2" on the second one. Is there any mistake in the syntax or in my understanding of the SetString function ?
List.SetString(i, s) is the equivalent of Delphi's List := s
Here the items 0 and 1 do not exist, so they cannot be modified.
You have to do List.Add(s) or List.Insert(i, s) to insert an item in the list
List.Add works perfect for my purposes (I need it for a new version of the "update scripts" script).
There's another problem: I want to load text from a file on harddisk, but I do not know if the file is present in the folder.. if the file is missing, the script stops and shows a message "Cannot open file "c:\xxx ...". I found the Delphi command "try / except / finally" and tried
try
List := TStringList.Create;
List.LoadFromFile(dirScripts + URLDecode(ScriptName));
except
on E : Exception do
showmessage('error');
end;
and all kinds of similar syntax or different exceptions (EAccessViolation,...), but nothing works. Do I need a specific exception for that or does it not work from within a AMC script ?
Exceptions are not handled by the scripting engine, unfortunately.
I think that the only solution would be to launch a command to create the file if it does not exist (cmd.exe /c if not exist file.txt copy nul file.txt)