Topic: Process text file

Hi, I want to create a function that allows me to do this.
1- read a sql file
2- process the read (clean data), example, delete the lines that start with a certain character.
3- store the processed in a variable
show the displayed in a text box

tks

Re: Process text file

Hello.


Example:

function OpenSQLFile(FileName: string): string;
var
    sl: TStringList;
    i,c: integer;
begin
    sl := TStringList.Create;
    sl.LoadFromFile(FileName);

    c := sl.Count - 1;
    for i := c downto 0 do
    begin
        if Pos('abc', sl[i])=1 then sl.Delete(i);
    end;

    result := sl.Text;
    sl.Free;
end;


procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
var
    s: string;
begin
    s := OpenSQLFile('d:\sql.txt');
    Form1.Memo1.Text := s;
end;
Dmitry.

Re: Process text file

Hello wenchester21,


Just one thing to add to what Dmitry answered.


You might have noticed that Dmitry used

for i := c downto 0 do

This is very important to process your StringList in reversed order like Dmitry said because if you happen to delete some strings (lines), the order of the stringlist changes and you might miss some of the strings. The reverse order processing of the list avoids this problem.


Have a good day


Math

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: Process text file

Hello Dmitry and mathmathou

I've tried it and added it to my solution and it's great for me now, if I have any questions, I'll tell you.

Thanks and regards