我想那一个文本文件的符合某一条件的所有行取出来,然后写入另外一个文件,请问怎样实现这一操作?
要考虑到下列情况:
例如某一行 0200 张三 412545212452125635 03001425
该行在写入新文件时要字段间的空字符应相等(即:间距一致),谢谢指教!
要考虑到下列情况:
例如某一行 0200 张三 412545212452125635 03001425
该行在写入新文件时要字段间的空字符应相等(即:间距一致),谢谢指教!
调试欢乐多
writeln(F,s);
writeln
dm.QryLog.First;
while not dm.QryLog.Eof do
begin
s:=.....
writeln(F,s);
dm.QryLog.Next;
end;
CloseFile(F);
begin
str_spath := frm_main.LEdt_Spath.Text;
if not DirectoryExists(str_spath) then
begin
ShowMessage('Cann''t find dir '+str_spath+'!');
Result := false;
exit;
end;
Result := true;
end;function RdFile(str_sourcef:string):string;
var
str_sn, str_sid: string;
begin
AssignFile(f_s,str_sourcef);
Reset(f_s);
While not AnsiContainsText(str_sn,'serial') do
begin
if Eof(f_s) then
begin
ShowMessage('Cann''t find sn and sid!');
exit;
end;
Readln(f_s,str_sn);
end;
str_sn := Trim(UpperCase(RightStr(str_sn,Length(str_sn)-Pos(':',str_sn))));
Reset(f_s);
While not AnsiContainsText(str_sid,'system') do
Readln(f_s,str_sid);
str_sid := Trim(UpperCase(RightStr(str_sid,Length(str_sid)-Pos(':',str_sid))));
CloseFile(f_s);
Result := str_sn+','+str_sid+',';
end;procedure Tfrm_main.BBtn_StartClick(Sender: TObject);
var
f_t: Tstrings; rec_search: TSearchREC; str_content,str_tf: string;
begin
BBtn_Start.Enabled := false;
BBtn_Close.Enabled := false;
If ChkFile = false then
begin
frm_main.LEdt_Spath.SetFocus;
exit;
end;
str_tf := GetCurrentDir()+'\'+FormatDateTime('yyyymmddmm',Now)+'.csv';
Lb_status.Caption := str_tf+' will be built!';
f_t := TstringList.Create;
if FileExists(str_tf) then
begin
If Application.MessageBox('File is already exist, append?','Confirm',MB_YesNo) = 7 then
begin
ShowMessage('Pls. move this file to other place,and press ''Start'' button again!');
BBtn_Start.Enabled := true;
BBtn_Close.Enabled := true;
exit;
end
else
f_t.LoadFromFile(str_tf);
end;
if FindFirst(str_spath+'\*.txt', faAnyFile, rec_search) = 0 then
begin
repeat
str_content := RdFile(str_spath+'\'+rec_search.Name);
f_t.Append(str_content);
f_t.SaveToFile(str_tf);
until FindNext(rec_search) <> 0;
end;
FindClose(rec_search);
f_t.Free;
Lb_Status.Caption := str_tf+' is built complete!';
BBtn_Start.Enabled := true;
BBtn_Close.Enabled := true;
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, StrUtils;type
Tfrm_main = class(TForm)
LEdt_Spath: TLabeledEdit;
BBtn_Start: TBitBtn;
BBtn_Close: TBitBtn;
Lb_Status: TLabel;
procedure BBtn_StartClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
frm_main: Tfrm_main; str_spath, str_sfile, str_tfile: String; f_s: TextFile;implementation{$R *.dfm}