怎样循环输入文件?(100分)

  • 主题发起人 主题发起人 lizheng
  • 开始时间 开始时间
L

lizheng

Unregistered / Unconfirmed
GUEST, unregistred user!
我想让程序循环查找E盘上的文件,并将其写入一个文件中。
procedure TForm1.Button2Click(Sender: TObject);
var
s:string;
f:textfile;
begin
FindFirst('e:/unit/*.*', faAnyFile, SearchRec);
Label1.Caption := SearchRec.Name + ' is ' + IntToStr(SearchRec.Size) + ' bytes in size';
while FindNext(SearchRec) = 0 do
begin
Label1.Caption := SearchRec.Name + ' is ' + IntToStr(SearchRec.Size) + ' bytes in size';
s:=string(Searchrec.Name);
append(F);
writeln(F,s);
end;
FindClose(SearchRec);
PostQuitMessage(0);
end;
FindClose(SearchRec);
end;
上述循环达不到我想要的目的,但是如果按下面的执行便能达到目的。
var
s,path:string
f:textfile;
procedure TForm1.Button1Click(Sender: TObject);
begin
FindFirst(path, faAnyFile, SearchRec);
Label1.Caption := SearchRec.Name + ' is ' + IntToStr(SearchRec.Size) + ' bytes in size';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if FindNext(SearchRec) = 0 then
begin
Label1.Caption := SearchRec.Name + ' is ' + IntToStr(SearchRec.Size) + ' bytes in size';
s:=string(Searchrec.Name);
append(F);
writeln(F,s);
end
else
FindClose(SearchRec);
end;



procedure TForm1.FormCreate(Sender: TObject);
begin
path:='e:/unit/*.*';
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
AssignFile(F, OpenDialog1.FileName);
end;

end;
 
我有一个控件专门作递归遍历磁盘文件的工作,如果需要Mail我。
 
查找完一种类型,是否需要将搜索路警复位!?就象文件的Reset(FilVar)那样。
 
我按照你的想法做了一下,没问题的!

procedure TForm1.Button4Click(Sender: TObject);
var
f :textfile;
begin
AssignFile(F, 'd:/test.txt');
Append(F);
FindFirst('d:/temp/*.*', faAnyFile and not faDirectory, SearchRec);
Label1.Caption := SearchRec.Name + ' is ' +
IntToStr(SearchRec.Size) + ' bytes in size';
Writeln(F,SearchRec.Name);
while FindNext(SearchRec) = 0 do
begin
Label1.Caption := SearchRec.Name + ' is ' +
IntToStr(SearchRec.Size) + ' bytes in size';
Writeln(F,SearchRec.Name);
end;
FindClose(SearchRec);
CloseFile(F);
end;
 
<pre><font size=3>
procedure TForm1.Button1Click(Sender: TObject);
var s:string;
f:textfile;
searchrec:tsearchrec; //此处加上
begin
AssignFile(F, 'c:/test.txt'); //此处加上
Rewrite(F); //此处改为
FindFirst('c:/*.*', faAnyFile, SearchRec);
Label1.Caption := SearchRec.Name + ' is ' +
IntToStr(SearchRec.Size) + ' bytes in size';
while FindNext(SearchRec) = 0 do begin
Label1.Caption := SearchRec.Name + ' is ' +
IntToStr(SearchRec.Size) + ' bytes in size';
s:=string(Searchrec.Name);
append(F);
writeln(F,s);
end;
FindClose(SearchRec);
PostQuitMessage(0);
// end; 此处去掉
FindClose(SearchRec);
end;
</font></pre>
 
SearchRec :TSearchRec; 定义为全局或局部!
 
多谢各位大虾!!!
 
Thanks a lot!
 
后退
顶部