文件查找例子:
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 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;
解决方案 »
- delphi如何获得一个文件夹下所有文件的信息?
- 有关数据库的问题?
- Could not convert variant of type(Dispatch) into type(Integer){连接组件出现这样的错误}
- delphi+access中的ADODataset为什么不支持substring函数
- 关于MDI窗口背景图的加载问题?
- 天啊!Delphi7的打印控件怎么使用呀?
- 哪位有Delphi 5.X分布式多层应用系统篇(李维)书中的源码(不是书)?
- 打包问题
- 请问如何通过代码实现读取doc文件以及存储为doc文件?
- Delphi中如何在往地址0x304写入数据?在C中是用outportb('0x304',1)实现的。
- 是否DelPhi在数据库方面的一个BUG?
- delphi6 的两个补丁的问题.
依赖心不要太强了,呵呵 :-)
1. 获取当前目录下的所有下一级子目录。
2. 存入字符串列表中(Tstrings)。
其中,用到了几个API函数。
FindFirst 是找出指定目录下第一个文件或目录。
FindNext 一般和FindFirst配合使用,用来找出下一个文件或目录。
FindClose 用来关闭查询。
(以上函数Delphi在线帮助中有详尽解释,在此不赘述);
3. 用FileExists函数查找当前目录,
4. 寻找是否有满足条件的文件存在,
5. 依次使各个子目录成为当前目录,
6. 递归调用本函数,
7. 释放资源,
8. 返回查询结果。
代码如下:
1. 从搜索记录中判断是否是子目录。
function IsValidDir(SearchRec:TSearchRec):Boolean;
begin
if (SearchRec.Attr=16) and
(SearchRec.Name<>'.') and
(SearchRec.Name<>'..') then
Result:=True
else
Result:=False;
end;
2. 这是查询主体函数。
参数介绍:Mainpath: 指定的查询目录。
Filename: 欲查询的文件。
Foundresult: 返回的含完整路径的匹配文件(可能有多个)。如果有匹配文件,函数返回True,否则,返回False; function SearchFile(mainpath:string; filename:string;
var foundresult:TStrings):Boolean;
var i:integer;
Found:Boolean;
subdir1:TStrings;
searchRec:TsearchRec;
begin
found:=false;
if Trim(filename)<>'' then
begin
subdir1:=TStringList.Create;//字符串列表必须动态生成
//找出所有下级子目录。
if (FindFirst(mainpath+'*.*', faDirectory, SearchRec)=0) then
begin
if IsValidDir(SearchRec) then
subdir1.Add(SearchRec.Name);
while (FindNext(SearchRec) = 0) do
begin
if IsValidDir(SearchRec) then
subdir1.Add(SearchRec.Name);
end;
end;
FindClose(SearchRec);
//查找当前目录。
if FileExists(mainpath+filename) then
begin
found:=true;
foundresult.Add(mainpath+filename);
end;
//这是递归部分,查找各子目录。
for i:=0 to subdir1.Count-1 do
found:=Searchfile(mainpath+subdir1.Strings[i]+
'\',Filename,foundresult)or found;
//资源释放并返回结果。
subdir1.Free;
end;
result:=found;
end;
总之,只要掌握了思路,用哪种编程语言都可以实现。现在,你可以轻松的给你 的系统挂上一个非常使用的功能了。
SOS!!!!