Page 1 of 1

TStringList : SetString

Posted: 2007-07-24 19:29:28
by bad4u
I've got some problems using the SetString function :

Code: Select all

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 ? :)

Posted: 2007-07-25 08:31:40
by antp
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

Posted: 2007-07-25 18:05:58
by bad4u
Thanks a lot :)

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

Code: Select all

          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 ?

Maybe you have an idea about that ?

Posted: 2007-07-26 13:47:15
by antp
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)

Posted: 2007-07-27 06:16:53
by bad4u
To create a new file does not help me, because I wanted to use the exception to check if a script does exist in scripts folder without cmd.exe ;)

Thanks, I'll have to choose an other way then.