Function FinfFirst(Const Path:String;Attr:Word;var SearchRec:TSearchRec):Integer; TSearchRec=record Fill:array [1..21] of Byte; Attr:Byte; Time:Longint; Size:Longint; Name:string[12]; end; 其中Name为匹配的文件名。 函数返回0,表示查找成功,否则返回DOS错误代码的负值。-18表示没有找到匹配的文件。
更正:上面的函数名写错了finffirst-->findfirst Attr为查找文件的属性
下面说明:C:\1这个目录是否存在txt文件. procedure TForm1.Button1Click(Sender: TObject); var SearchRec:tSearchRec; FindResult:integer; begin FindResult := FindFirst('C:\1\'+ '*.txt',FaAnyFile,SearchRec); if findresult=0 then showmessage('存在txt文件') else showmessage('不存在txt文件'); end;
to liugaqiong(may snow): *.*也是可以的了.不过我没有试过.SeaWave() 说的也许是正确的吧!你自己试试吧!程序并不复杂.程序后是要释放seachrec的资源: FindClose(SearchRec);
function TForm1.IsHasFile(const Path:string):boolean;//返回true代表有文件 var sr: TSearchRec; Attr:integer; iResult:integer; begin result:=false;//初始化,假定没有文件 Attr:=faAnyFile-faDirectory ;//可以为其它属性(具体见Delphi帮助) iResult:= FindFirst(Path+'\*.*',Attr,sr);//检测目录'G:\1\'下面是否有文件 while iResult=0 do begin if (sr.Name <>'.') and (sr.Name <>'..') then //可以去掉 result:=true;//代表有文件 iResult:=FindNext(sr); end; FindClose(sr);end;
TSearchRec=record
Fill:array [1..21] of Byte;
Attr:Byte;
Time:Longint;
Size:Longint;
Name:string[12];
end;
其中Name为匹配的文件名。
函数返回0,表示查找成功,否则返回DOS错误代码的负值。-18表示没有找到匹配的文件。
Attr为查找文件的属性
procedure TForm1.Button1Click(Sender: TObject);
var
SearchRec:tSearchRec;
FindResult:integer;
begin
FindResult := FindFirst('C:\1\'+ '*.txt',FaAnyFile,SearchRec);
if findresult=0 then
showmessage('存在txt文件')
else
showmessage('不存在txt文件');
end;
findnext
记得要释放seachrec的资源哟
*.*也是可以的了.不过我没有试过.SeaWave() 说的也许是正确的吧!你自己试试吧!程序并不复杂.程序后是要释放seachrec的资源:
FindClose(SearchRec);
var
sr: TSearchRec;
Attr:integer;
iResult:integer;
begin
result:=false;//初始化,假定没有文件
Attr:=faAnyFile-faDirectory ;//可以为其它属性(具体见Delphi帮助)
iResult:= FindFirst(Path+'\*.*',Attr,sr);//检测目录'G:\1\'下面是否有文件
while iResult=0 do
begin
if (sr.Name <>'.') and (sr.Name <>'..') then //可以去掉
result:=true;//代表有文件
iResult:=FindNext(sr);
end;
FindClose(sr);end;