请教怎么查找文件

xiaofeixia110 2003-10-15 04:21:59
大家好,我刚才学DELPHI不久。现在我想问问怎么查找硬盘里的某一指定文件
另久我不知如何判断是否目录还是文件。大家帮帮我,谢谢

我看过网上的文章。实现不了。
...全文
71 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
pankun 2003-10-16
  • 打赏
  • 举报
回复
是查找某一文件是否存在,还是查找文件啊?
下面是查找指定文件的例子.

procedure findall(disk,path: String; var fileresult: Tstrings);
var

fpath: String;

fs: TsearchRec;

begin

fpath:=disk+path+'\*.*';

if findfirst(fpath,faAnyFile,fs)=0 then

begin

if (fs.Name<>'.')and(fs.Name<>'..') then

if (fs.Attr and faDirectory)=faDirectory then

findall(disk,path+'\'+fs.Name,fileresult)

else

fileresult.add(disk+strpas(strupper(pchar(path)))+'\'+strpas(

strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')');

while findnext(fs)=0 do

begin

if (fs.Name<>'.')and(fs.Name<>'..') then

if (fs.Attr and faDirectory)=faDirectory then

findall(disk,path+'\'+fs.Name,fileresult)

else

fileresult.add(disk+strpas(strupper(pchar(path)))+'\'+str

pas(strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')');

end;

end;

findclose(fs);

end;

procedure DoSearchFile(Path: string; Files: TStrings = nil);

var

Info: TSearchRec;

procedure ProcessAFile(FileName: string);

begin

if Assigned(PnlPanel) then

PnlPanel.Caption := FileName;

Label2.Caption := FileName;

end;

function IsDir: Boolean;

begin

with Info do

Result := (Name <> '.') and (Name <> '..') and ((attr and fadirectory) = fadirectory);

end;

function IsFile: Boolean;

begin

Result := not ((Info.Attr and faDirectory) = faDirectory);

end;

begin

Path := IncludeTrailingBackslash(Path);

try

if FindFirst(Path + '*.*', faAnyFile, Info) = 0 then

if IsFile then

ProcessAFile(Path + Info.Name)

else if IsDir then DoSearchFile(Path + Info.Name);

while FindNext(Info) = 0 do

begin

if IsDir then

DoSearchFile(Path + Info.Name)

else if IsFile then

ProcessAFile(Path + Info.Name);

Application.ProcessMessages;

if QuitFlag then Break;

Sleep(100);

end;

finally

FindClose(Info);

end;

end;
xinglinshu 2003-10-16
  • 打赏
  • 举报
回复
用FileExists(const FileName: string): Boolean;查

5,379

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧