Delete all items that are after a specific item

Hello,

I would like that when I launch my script, if a item of type “Playlist” is detected, all items which follow this specific item are removed from the playlist.

I started to develop the script, how can I make it work properly and optimize it? Is the double loop a good idea?

Thank you

var
  i: integer;
  i2: integer;

begin
  CurrentPlaylist.BeginUpdate;
  try
    for i := CurrentPlaylist.GetNextIndex to CurrentPlaylist.GetCount - 1 do
      if CurrentPlaylist.GetItem(i).GetItemType = pitPlaylist then begin
        i2 := i;
        break;
      end;
    for i2 := i to CurrentPlaylist.GetCount - 1 do
      CurrentPlaylist.Delete(i2);
  finally
    CurrentPlaylist.EndUpdate;
  end;
end.