Forum > Beginners

Repeat Actions until every File from Memo is done

(1/1)

chim:
Hello Pros,

could you please help me once again?  :-[

My code work for me just fine for one file, my problem is to get this actions to every file in my Memo3.

To make this clear what my App does:

I got a HTML Template, and i would change seperate parts of the file with new ones, and save this as a new file.

1) Choose Template
2) Template get loaded into Memo1
3) Pressing the Button will search and list every HTML File from the dir you choose into Memo3
4) Load into Memo4 the current file listed at first in Memo3
5) Load HTML Code from prelisted locations of Memo4 and write it to Memo2
6) Write the HTML Code from Memo2 into Memo1 to the prelistet locations
7) Save File and repeat the Step 4) to Step 7) until every file from Memo3 was done

So thats my plan, unfortunately it does this actions just one time, and i can't figure out why  :(

Could you please help me again?

here is the code:

Yes i know its a mess  :-[




--- Code: ---procedure TForm1.Button2Click(Sender: TObject);
var
  i: integer;
  Ini: TIniFile;
  Pfad: String;
  Pfad2: Integer;
  Save: String;
  begin
    If comboBox1.Text ='Vorlage wählen...' then
begin
showmessage('Bitte Vorlage wählen!');
end
else
If comboBox1.Text ='Normalglas' then
begin
  if SelectDirectoryDialog1.Execute then
       begin
       GetFilesInDirectory(SelectDirectoryDialog1.FileName, '*.html', Memo3.Lines, True, True);
begin
for i:=0 to Memo3.Lines.Count-1 do
  Ini:=TIniFile.Create(GetApplicationDirectory+'Vorlagen.ini');
  try
     Pfad := Ini.ReadString('Normalglas', 'Pfad', '');
     Memo1.Lines.LoadFromFile(GetApplicationDirectory+Pfad);
  finally
    Ini.Free;
       begin
          Memo4.Lines.LoadFromFile(Memo3.Lines[i]);
          Memo2.Lines.Add(String(Memo4.Lines[12]));
          Memo2.Lines.Add(String(Memo4.Lines[14]));
          Memo2.Lines.Add(String(Memo4.Lines[15]));
          Memo2.Lines.Add(String(Memo4.Lines[38]));
          Memo2.Lines.Add(String(Memo4.Lines[50]));
          Memo2.Lines.Add(String(Memo4.Lines[55]));
          with ProgressBar1 do
       begin
       Ini:=TIniFile.Create(GetApplicationDirectory+'Vorlagen.ini');
       try
       Pfad2 := Ini.ReadInteger('Normalglas', 'Platzhalter1', 10);
       Memo1.Lines[Pfad2]:= Memo2.Lines[0];
       ProgressBar1.Position := 15;
       Pfad2 := Ini.ReadInteger('Normalglas', 'Platzhalter2', 12);
       Memo1.Lines[Pfad2]:= Memo2.Lines[1];
       ProgressBar1.Position := 30;
       Pfad2 := Ini.ReadInteger('Normalglas', 'Platzhalter3', 13);
       Memo1.Lines[Pfad2]:= Memo2.Lines[2];
       ProgressBar1.Position := 45;
       Pfad2 := Ini.ReadInteger('Normalglas', 'Platzhalter4', 36);
       Memo1.Lines[Pfad2]:= Memo2.Lines[3];
       ProgressBar1.Position := 60;
       Pfad2 := Ini.ReadInteger('Normalglas', 'Platzhalter5', 48);
       Memo1.Lines[Pfad2]:= Memo2.Lines[4];
       ProgressBar1.Position := 75;
       Pfad2 := Ini.ReadInteger('Normalglas', 'Platzhalter6', 53);
       Memo1.Lines[Pfad2]:= Memo2.Lines[5];
       ProgressBar1.Position := 90;
         finally
         Ini.Free;
         Save:=(Memo3.Lines[0]+'glas.html');
           Memo1.Lines.SaveToFile(Save);
           ProgressBar1.Position := 100;
end;
end;
end;
end;
  end;
  end;
  end;
  end;
--- End code ---

howardpc:

--- Quote from: chim on September 24, 2014, 11:20:33 am ---here is the code:
Yes i know its a mess  :-[

--- End quote ---

Your main problem is one of organisation. You don't share enough code for us to be of much help in detail, but since you have troubled to outline the steps you want to accomplish, why not structure your program to reflect orderly execution of those steps?
Separating discrete functionalities into self-contained methods makes pinpointing bugs much easier than trying to unravel a monolithic button-click procedure which references so many other GUI controls and files all together in what is termed a 'close-coupled' way. You end up with something I believe you call Kabelsalat...
Something along these lines (adapt to suit):

--- Code: --- TForm1 = class(TForm)
    BUpdateTemplate: TButton;
    Memo1: TMemo;
    Memo2: TMemo;
    Memo3: TMemo;
    Memo4: TMemo;
    procedure BUpdateTemplateClick(Sender: TObject);
  private
    procedure LoadTemplateIntoMemo1;
    procedure ListFilesInMemo3;
    procedure DisplayFileInMemo4(anIndex: integer);
    procedure CopyHTMLtoMemo2;
    procedure ProcessMemo2ToMemo1AndSave;
    procedure UpdateAndSaveTemplate;
  end;     

...

implementation

...

procedure TForm1.BUpdateTemplateClick(Sender: TObject);
begin
  UpdateAndSaveTemplate;
end;

procedure TForm1.UpdateAndSaveTemplate;
var
  i: integer;
begin
  LoadTemplateIntoMemo1;
  ListFilesInMemo3;
  for i:=0 to memo3.Lines.Count - 1 do begin
    DisplayFileInMemo4(i);
    CopyHTMLtoMemo2;
    ProcessMemo2ToMemo1AndSave;
  end;
end;   

...

--- End code ---

chim:
Okay Thank you, i try to structure it this way

chim:
Man Howardpc THANK YOU!!!!!

you're the man!

i accomplished my work with your help.

through your structure sample i figure out 2 problems, so now it works flawless 8-)

Navigation

[0] Message Index

Go to full version