⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.pas

📁 文本编辑例子.可讲批量的处理想要的文本到界面里
💻 PAS
字号:
unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ImgList, ToolWin;

type
  TForm1 = class(TForm)
    ToolBar1: TToolBar;
    ImageList1: TImageList;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ListBox1: TListBox;
    Panel1: TPanel;
    ProgressBar1: TProgressBar;
    ToolButton4: TToolButton;
    StatusBar1: TStatusBar;
    OpenDialog1: TOpenDialog;
    Splitter1: TSplitter;
    Memo1: TMemo;
    ComboBox1: TComboBox;
    Button1: TButton;
    Button2: TButton;
    procedure ToolButton1Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button2Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private

    { Private declarations }
  public

    { Public declarations }
    procedure MakeListBox;
    procedure SortList;
  end;

var
  Form1: TForm1;
procedure firstFun();
implementation

uses RegExpr;

{$R *.dfm}

var
  tmpFile:TStringList;

procedure firstFun();
var
IsExists:boolean;
begin
IsExists := FileExists('DEVICE.VAYO');
if not IsExists then begin
showmessage('File not exist!');
exit;
end;
Application.CreateForm(TForm1, Form1);
end;


procedure TForm1.ToolButton1Click(Sender: TObject);
begin
  StatusBar1.Panels[0].Text:='';
  OpenDialog1.FileName:='';
  OpenDialog1.Execute;
  if OpenDialog1.FileName='' then Exit;
  tmpFile.Clear;
  tmpFile.LoadFromFile(OpenDialog1.FileName);
  //tmpFile.LoadFromFile('DEVICE.VAYO');
 StatusBar1.Panels[0].Text:='File: '+OpenDialog1.FileName;
  //StatusBar1.Panels[0].Text:='File: '+'DEVICE.VAYO';
  MakeListBox;
  SortList;
end;

procedure TForm1.MakeListBox;
var
  i:Integer;
  r:TRegExpr;
  tmpStr:String;
begin
  ListBox1.Clear;
  i:=0;
  r:=TRegExpr.Create;
  ProgressBar1.Max:=tmpFile.Count;
  while i<tmpFile.Count do begin
    if ExecRegExpr('^FIL=".*"$',tmpFile[i]) then begin
      tmpStr:='';
      r.Expression:='^FIL="(.*)"$';
      r.Exec(tmpFile[i]);
      tmpStr:=r.Substitute('$1');
      if tmpStr<>'' then ListBox1.Items.Add(tmpStr);
      ProgressBar1.Position:=i;
    end;
    Inc(i);
  end;
  ProgressBar1.Position:=0;
  FreeAndNil(r);
end;

procedure TForm1.SortList;
var
  tmpList:TStringList;
begin
  tmpList:=TStringList.Create;
  tmpList.Assign(ListBox1.Items);
  tmpList.Sort;
  ListBox1.Items.Assign(tmpList);
  FreeAndNil(tmpList);
end;

procedure TForm1.ListBox1Click(Sender: TObject);
var
  i,j,k:Integer;
begin
  for j:=0 to ListBox1.Count-1 do
    if ListBox1.Selected[j] then begin
      for i:=0 to tmpFile.Count-1 do begin
        if ExecRegExpr('^FIL="'+ListBox1.Items[j]+'"$',tmpFile[i]) then begin
          k:=i+1;
          Memo1.Clear;
          while not ExecRegExpr('^:EOD$',tmpFile[k]) do begin
            Memo1.Lines.Add(tmpFile[k]);
            Inc(k);
          end;
          Break;
        end;
      end;
      Break;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i,j,k:Integer;
  isHaveField:Boolean;
  insertField,tmpField:String;
  r:TRegExpr;
begin
  r:=TRegExpr.Create;
  insertField:='';
  r.Expression:='^(.*)=.*$';
  r.Exec(ComboBox1.Text);
  insertField:=r.Substitute('$1');
  if insertField='' then begin
    FreeAndNil(r);  Exit;
  end;
  ProgressBar1.Max:=ListBox1.Count;
  for j:=0 to ListBox1.Count-1 do begin
    if ListBox1.Selected[j] then
      for i:=0 to tmpFile.Count-1 do begin
        if ExecRegExpr('^FIL="'+ListBox1.Items[j]+'"$',tmpFile[i]) then begin
          k:=i+1;
          while not ExecRegExpr('^:EOD$',tmpFile[k]) do begin
            r.Expression:='^(.*)=.*$';
            r.Exec(tmpFile[k]);
            tmpField:=r.Substitute('$1');
            isHaveField:=False;
            if tmpField=insertField then begin
              tmpFile.Delete(k);
              tmpFile.Insert(k,ComboBox1.Text);
              isHaveField:=True;
            end;
            Inc(k);
          end;
          if not isHaveField then tmpFile.Insert(k,ComboBox1.Text);
          Break;
        end;
      end;
    ProgressBar1.Position:=j+1;
  end;
  FreeAndNil(r);
  tmpFile.SaveToFile(OpenDialog1.FileName);
  ProgressBar1.Position:=0;
  ListBox1Click(Sender);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  FreeAndNil(tmpFile);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  i,j,k:Integer;
  r:TRegExpr;
begin
  if ComboBox1.Text='' then Exit;
  r:=TRegExpr.Create;
  ProgressBar1.Max:=ListBox1.Count;
  for j:=0 to ListBox1.Count-1 do begin
    if ListBox1.Selected[j] then
      for i:=0 to tmpFile.Count-1 do begin
        if ExecRegExpr('^FIL="'+ListBox1.Items[j]+'"$',tmpFile[i]) then begin
          k:=i+1;
          while not ExecRegExpr('^:EOD$',tmpFile[k]) do begin
            if tmpFile[k]=ComboBox1.Text then tmpFile.Delete(k)
            else Inc(k);
          end;
          Break;
        end;
      end;
    ProgressBar1.Position:=j+1;
  end;
  FreeAndNil(r);
  tmpFile.SaveToFile(OpenDialog1.FileName);
  //tmpFile.SaveToFile('DEVICE.VAYO');
  ProgressBar1.Position:=0;
  ListBox1Click(Sender);
end;

procedure TForm1.FormShow(Sender: TObject);
//var
//IsExists: Boolean;
begin
tmpFile:=TStringList.Create;
 // IsExists: Boolean;
//  IsExists := FileExists('DEVICE.VAYO');
//   if not IsExists then exit;

  StatusBar1.Panels[0].Text:='';
  OpenDialog1.FileName:='DEVICE.VAYO';
  if OpenDialog1.FileName='' then Exit;
  tmpFile.Clear;
  tmpFile.LoadFromFile(OpenDialog1.FileName);
  //tmpFile.LoadFromFile('DEVICE.VAYO');
 StatusBar1.Panels[0].Text:='File: '+OpenDialog1.FileName;
  //StatusBar1.Panels[0].Text:='File: '+'DEVICE.VAYO';
  MakeListBox;
  SortList;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -