代码还是那天的代码,我再贴一次吧!
{---------------------------------------
文件查找函数 MySearchFile
参数说明:
sFileName: 要查找的文件名称
sRootDir: 指定在哪个目录中查找
bFileOnly: 是否只查找文件
bRecurse: 是否查找子目录
返回值:
FileList: 查找结果列表
----------------------------------------}
function MySearchFile(sFileName, sRootDir: string;
bFileOnly, bRecurse: boolean): TStringList;
//内嵌文件查找递归过程
procedure DoSearchFile(sFileName, sRootDir: string;
bFileOnly, bRecurse: boolean;
var FileList: TStringList);
var
Found: integer;
SearchRec: TSearchRec;
begin
//开始查找
Found := FindFirst(sRootDir + '/*.*', faAnyFile, SearchRec);
while Found = 0 do
begin
//遇到子目录时确定是否查找子目录和是否将符合条件的子目录加入查找结果
if ((SearchRec.Attr and faDirectory) = faDirectory) and (SearchRec.Name <> '.') and
(SearchRec.Name <> '..') then
begin
if (AnsiCompareText(sFileName, SearchRec.Name) = 0) and not bFileOnly then
FileList.Add(sRootDir + '/' + SearchRec.Name);
if bRecurse then
DoSearchFile(sFileName, sRootDir + '/' + SearchRec.Name, bFileOnly, bRecurse, FileList);
end
//找到符合条件的文件时加入查找结果
else if AnsiCompareText(sFileName, SearchRec.Name) = 0 then
FileList.Add(sRootDir + '/' + SearchRec.Name);
Found := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;
var
FileList: TStringList;
begin
FileList := TStringList.Create;
FileList.Clear;
DoSearchFile(sFileName, sRootDir, bFileOnly, bRecurse, FileList);
Result := FileList;
end;
调用:
s := MySearchFile('aaa.txt','D:',true,true);
不过今天我看了这个98系统唯一一个可疑的地方,就是在资源管理器中所有的文件都看不
见后缀名!