Показать сообщение отдельно
  #1  
Старый 27.10.2010, 09:20
Hardip Hardip вне форума
Прохожий
 
Регистрация: 20.03.2008
Сообщения: 6
Репутация: 10
По умолчанию Замена строк в файлах

Вот программа по замене строки в html файлах. В ней выбор папки и простейший поиск нужных строк (берущихся из edit) и замена на нужное значение. Есть так же ограничение по выбору (просто в папке файлы - 1800 штук), нужно чтобы можно было ввести в edit не одно значение (j), а несколько (например - j, ba,ca, bg, vp, vp* - где звездочка может быть любое количество букв или симоволов). Вот чтобы он один считывал я осилил))) А вот как седлать чтобы можно было вводить несколько значений напрмер через запятую?


Код:
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, FileCtrl, ComCtrls, AppEvnts;

type
  TForm2 = class(TForm)
    Edit1: TEdit;
    Label1: TLabel;
    FileListBox1: TFileListBox;
    DirectoryListBox1: TDirectoryListBox;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    Edit3: TEdit;
    Button1: TButton;
    ProgressBar1: TProgressBar;
    Label4: TLabel;
    Label5: TLabel;
    Edit4: TEdit;
    Edit5: TEdit;
    Label6: TLabel;
    Edit6: TEdit;
    ApplicationEvents1: TApplicationEvents;
    Memo1: TMemo;
    Memo2: TMemo;
    Label7: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure zam(fn:string);
    procedure Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
    procedure FormActivate(Sender: TObject);
    procedure Edit6KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure DirectoryListBox1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;
  fname:string;
  poisk1,zamena1,poisk2,zamena2,s:string;
  dlstrp1,dlstrz1,dlstrp2,dlstrz2,i,isk:integer;

implementation

{$R *.dfm}

procedure TForm2.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
begin
Label6.Caption:='Количесво файлов '+inttostr(FileListBox1.Count-isk);
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  s:string;
begin
poisk1:=edit2.Text;
zamena1:=edit3.Text;
dlstrp1:=Length(poisk1);
dlstrz1:=Length(zamena1);

poisk2:=edit4.Text;
zamena2:=edit5.Text;
dlstrp2:=Length(poisk2);
dlstrz2:=Length(zamena2);
ProgressBar1.Max:=FileListBox1.Count-isk;
for I := 0 to FileListBox1.Count - 1 do
                       begin
                       fname:=FileListBox1.Items[i];
                       s:=FileListBox1.Items[i];
                       if s[1]<>edit6.text[1] then begin
                       ProgressBar1.Position:=i+1;
                       zam(fname);
                                                    end;
                       end;
ShowMessage('Готово !');
end;

procedure TForm2.DirectoryListBox1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
isk:=0;
 for I := 0 to FileListBox1.Count - 1 do
                       begin
                        s:=FileListBox1.Items[i];
                       if s[1]=edit6.text[1] then isk:=isk+1;
                       end;
Label6.Caption:='Количесво файлов '+inttostr(FileListBox1.Count-isk);
ProgressBar1.Max:=FileListBox1.Count-isk;
end;

procedure TForm2.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
FileListBox1.Mask:=Edit1.Text;
FileListBox1.Refresh;
isk:=0;
 for I := 0 to FileListBox1.Count - 1 do
                       begin
                        s:=FileListBox1.Items[i];
                       if s[1]=edit6.text[1] then isk:=isk+1;
                       end;
Label6.Caption:='Количесво файлов '+inttostr(FileListBox1.Count-isk);
ProgressBar1.Max:=FileListBox1.Count-isk;
end;

procedure TForm2.Edit6KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
isk:=0;
 for I := 0 to FileListBox1.Count - 1 do
                       begin
                        s:=FileListBox1.Items[i];
                       if s[1]=edit6.text then isk:=isk+1;
                       end;
end;

procedure TForm2.FormActivate(Sender: TObject);
begin
isk:=0;
 for I := 0 to FileListBox1.Count - 1 do
                       begin
                        s:=FileListBox1.Items[i];
                       if s[1]=edit6.text[1] then isk:=isk+1;
                       end;
Label6.Caption:='Количесво файлов '+inttostr(FileListBox1.Count-isk);
Button1.SetFocus;
end;

procedure tform2.zam(fn:string);
 var
 n1,n2,k:integer;
 stroka:string;
 begin
 memo1.Lines.LoadFromFile(fn);
   for k := 0 to memo1.Lines.Count - 1 do
    begin
      stroka:=memo1.Lines[k];
      n1:=pos(poisk1,stroka);

      if n1<>0 then begin
      delete(stroka,n1,dlstrp1);
      insert(zamena1,stroka,n1);
                   end;

      n2:=pos(poisk2,stroka);

      if n2<>0 then begin
      delete(stroka,n2,dlstrp2);
      insert(zamena2,stroka,n2);
                   end;

memo2.Lines.Insert(k,stroka);
   end;
   memo2.Lines.SaveToFile(fn);
   memo2.Clear;
   memo1.Clear;
end;


end.

http://imglink.ru/show-image.php?id=...da5ff7a1edbda5
Ответить с цитированием