|
|
Регистрация | << Правила форума >> | FAQ | Пользователи | Календарь | Поиск | Сообщения за сегодня | Все разделы прочитаны |
|
Опции темы | Поиск в этой теме | Опции просмотра |
#1
|
|||
|
|||
Замена строк в файлах
Вот программа по замене строки в 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 |
#2
|
||||
|
||||
Решений много, можно через комбобокс, можно как вы говорите придумать свой символ разделитель и уже дальше парсить полученную строку и искать текст. С примером vp* несколько непонятно, т.к. пробел,запятая,точка и т.д. тоже относятся к любым символам.
Жизнь такова какова она есть и больше никакова. Помогаю за спасибо. |
#3
|
|||
|
|||
Спасибо! Мне ещё не понятно вот что - вот сейчас он выбирает -"первую букву файлов исключений". Не уловлю где изменить чтобы выбирал 2, 3 ну сколько нужно...
|
#4
|
||||
|
||||
Вот вам вариант, разбирайтесь:
Код:
Type TReplace = record FindText: String; ReplaceText: String; end; Var Template: Array of TReplace; FileInfo: TSearchRec; InFile,OutFile: TextFile; s,FilePath: String; i: Integer; begin SetLength(Template, 3); Template[0].FindText := 'мама'; Template[0].ReplaceText := 'папа'; Template[1].FindText := 'мыла'; Template[1].ReplaceText := 'купил'; Template[2].FindText := 'раму'; Template[2].ReplaceText := 'машину'; FilePath := 'd:\1\'; if FindFirst(FilePath + '*.htm', faAnyFile, FileInfo) = 0 then begin try repeat if (FileInfo.Name = '.') or (FileInfo.Name = '..') or (FileInfo.Attr and faDirectory <> 0) then Continue; AssignFile(InFile, FilePath + FileInfo.Name); Reset(InFile); AssignFile(OutFile, FilePath + '~' + FileInfo.Name); Rewrite(OutFile); while not Eof(InFile) do begin ReadLn(InFile, s); for i := 0 to Length(Template) - 1 do s := StringReplace(s, Template[i].FindText, Template[i].ReplaceText,[rfReplaceAll]); WriteLn(OutFile, s); end; CloseFile(OutFile); CloseFile(InFile); until FindNext(FileInfo) <> 0; finally FindClose(FileInfo); end; end; end. Жизнь такова какова она есть и больше никакова. Помогаю за спасибо. |
#5
|
|||
|
|||
Спасибо огромное! Очень помогло!)) Разобрался и всё работает как мне надо)) Если дадите номер, могу кинуть $
|
#6
|
||||
|
||||
На здоровье. Читайте мою подпись
Жизнь такова какова она есть и больше никакова. Помогаю за спасибо. |