這是轉貼,具體是誰的我記不清楚了,這裡有你所要用到的技術,你再改變一下應該就可達到你的要求了□搜索整个目录 有时想要搜索整个目录,例如我编了个程序,要把硬盘上所有没用的文件找出来,列出来,然后删除. 就要用到第归法搜索整个给定目录.下面是例程: procedure scan(dir: string); var temp_name: string; full_name:string; s: TSearchRec; begin dir:= LowerCase (dir); if dir[Length(dir)]<> '\' then dir :=dir + '\'; temp_name := dir+'*.*'; if FindFirst (temp_name, faAnyFile, s) = 0 then repeat with s do begin Name := LowerCase (Name); if (Attr <> faDirectory) then begin full_name := dir + Name; ....... //在这里加入你想要对每个文件做的事. end else if ((Attr and faDirectory) <> 0) and ((Name <> '.') and (Name <> '..')) then scan(dir + Name);//如果是子目录,继续搜索.第归. end; until FindNext (s) <> 0; FindClose (s); end; end;
//取得子目录 procedure GetChildDir(Path:string;var ResultList:TStringList); var SearchRec : TSearchRec; Attr : integer; Found : integer; begin ResultList:=TStringList.Create; Attr := faDirectory; Found := FindFirst(Path, Attr, SearchRec); while Found = 0 do //表示有子目录 begin if (SearchRec.Attr=faDirectory) and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then ResultList.Add(SearchRec.Name); Found := FindNext(SearchRec); end; SysUtils.FindClose(SearchRec); end;
调用 var SkinList:TStringList; //皮肤目录列表 GetChildDir(ExtractFilePath(paramstr(0))+'skin\*.*',SkinList);
有时想要搜索整个目录,例如我编了个程序,要把硬盘上所有没用的文件找出来,列出来,然后删除.
就要用到第归法搜索整个给定目录.下面是例程:
procedure scan(dir: string);
var
temp_name: string;
full_name:string;
s: TSearchRec;
begin
dir:= LowerCase (dir);
if dir[Length(dir)]<> '\' then
dir :=dir + '\';
temp_name := dir+'*.*';
if FindFirst (temp_name, faAnyFile, s) = 0 then
repeat
with s do begin
Name := LowerCase (Name);
if (Attr <> faDirectory) then begin
full_name := dir + Name;
.......
//在这里加入你想要对每个文件做的事.
end
else if ((Attr and faDirectory) <> 0) and ((Name <> '.') and (Name <> '..'))
then scan(dir + Name);//如果是子目录,继续搜索.第归.
end;
until FindNext (s) <> 0;
FindClose (s);
end;
end;
procedure GetChildDir(Path:string;var ResultList:TStringList);
var SearchRec : TSearchRec;
Attr : integer;
Found : integer;
begin
ResultList:=TStringList.Create;
Attr := faDirectory;
Found := FindFirst(Path, Attr, SearchRec);
while Found = 0 do //表示有子目录
begin
if (SearchRec.Attr=faDirectory) and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
ResultList.Add(SearchRec.Name);
Found := FindNext(SearchRec);
end;
SysUtils.FindClose(SearchRec);
end;
var SkinList:TStringList; //皮肤目录列表
GetChildDir(ExtractFilePath(paramstr(0))+'skin\*.*',SkinList);