1

I'm currently working on a large script (for me) where I need do delete some files in a folder. To identify which files I need to delete, I'm reading a text file which contains all the informations I need. I'm using a TStringList class and the function Pos() to get these informations.

My problem comes from the function Pos() which is returning several values, one value for each string of the StringList (text file).

So here is the dedicated part of my script :

Procedure Delete;
Var
SL      : TStringList;
i, A, B : Integer;     //i is a Counter, A and B are not mandatory but are useful for DEBUG
PCBFile : String;
Begin
PCBFile := //Full Path
SL := TStringList.Create;
SL.LoadFromFile(PCBFile);
For i := 1 To (SL.Count - 1) Do         //I don't need to check the 1st line of the text file.
     A := Pos('gtl', SL.Strings[i]);    //Here is when the problem occurs.
     B := Pos('gbl', SL.Strings[i]);    //Doesn't get to here because i = SL.Count before looping
     If (A = 0) And (B = 0) Then        //The goal
          ChangeFileExt(PCBFile, '.TX');
          PCBFile := PCBFile + FloatToStr(i-2);     //The files I want to delete can have several extensions (tx1, tx2... txN)
          DeleteFile(PCBFile);
     End;
End;
SL.Free;
End;

If I change this line :

A := Pos('gtl', SL.Strings[i]);

To this :

ShowInfo (Pos('gtl', SL.Strings[i]));  //Basic function from Altium Designer, same as Writeln()

It will show a pop up with the result of the function Pos() for each line of the text file. I assume that SL.Strings[i] does an internal auto-increment of i, but I want to manage it with my For Do loop.

I past two days searching on the web but didn't find any clue on what to do, maybe the problem comes from Altium Designer, which is not really a programming software at first.

I also tried with the SL.IndexOf() function but it only works with strict Strings, which is not my case.

Thanks for your time and help.

Best regards, Jordan

  • 1
    You do realise that all three lines after `if ... then` need to be surrounded by a `begin ... end` block, otherwise the 2nd and 3rd lines will execute regardless of whether the `if ` condition is true? – MartynA Jun 09 '21 at 15:12
  • See my answer to [Proper structure syntax for Delphi/Pascal if then begin end and ;](https://stackoverflow.com/a/28221465/62576) for information about how to properly use them, which you don't here. The answer is written for Pascal, but applies equally as well to PascalScript and Delphi. – Ken White Jun 10 '21 at 01:23

1 Answers1

0

There are a few errors in the code, namely incomplete Begin .. End; blocks.

The first one is the for i .. loop. If you want to execute several statements within every iteration of the loop you must enclose them in a Begin .. End; pair. Your code is missing the Begin and therefore it assigns A SL.Count-1 times before it reaches the line where B is assigned. The second is after the If .. statement. I you want to execute several statements conditionally you must enclose them in a Begin .. End; pair after the If .. statement.

Add the two lines as marked below

For i := 1 To (SL.Count - 1) Do         //I don't need to check the 1st line of the text file.
Begin  // Add this line
    A := Pos('gtl', SL.Strings[i]);    //Here is when the problem occurs.
    B := Pos('gbl', SL.Strings[i]);    //Doesn't get to here because i = SL.Count before looping
    If (A = 0) And (B = 0) Then        //The goal
    Begin  // Add this line
        ChangeFileExt(PCBFile, '.TX');
        PCBFile := PCBFile + FloatToStr(i-2);     //The files I want to delete can have several extensions (tx1, tx2... txN)
        DeleteFile(PCBFile);
    End;
End;

Remember also that, in Pascal, indentation has no effect on how code executes (as it has in some other languages). Indentation is important for readability, but that is all.

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54