遍历目录,找到目录下所有文件
{文件路径+精确文件名+查找内容+是否替换+替换内容}
function TForm22.SearchDir(const Dir, FileType, SearchText: string;
IsReplace: boolean; const ReplaceText: string): boolean;
var
FS: TSearchRec;
FPath: String;
begin
FPath := IncludeTrailingPathDelimiter(Dir); {返回APath:\}
if FindFirst(FPath + '*.*', faAnyFile, FS) = 0 then {对指定的文件名及属性搜索目录,返回integer = 0表示找到文件}
begin
repeat //循环
if (FS.Name <> '.') and (FS.Name <> '..') then {FS.Name代表文件名}
begin
if ((FS.Attr and faDirectory) = faDirectory) then {判断是否是目录}
begin
SearchDir(FPath + FS.Name); {递归遍历目录}
end
else
begin
if ((LowerCase(ExtractFileExt(FS.Name)) = '.ini') and (FileType = '.ini')) then //找出需要的文件类型
begin
if SearchIniFile(FPath + FS.Name, SearchText, IsReplace, ReplaceText) then
begin
lbDirectories.Items.Add( FPath + FS.Name ); {加入到文件集中}
end;
end
else if ((LowerCase(ExtractFileExt(FS.Name)) = '.txt') and (FileType = '.txt')) then
begin
if SearchFile(FPath + FS.Name, SearchText, IsReplace, ReplaceText) then
begin
lbDirectories.Items.Add( FPath + FS.Name ); {加入到文件集中}
end;
end;
end;
end;
until FindNext(FS) <> 0; //查找下一关文件,直到找不到文件
SysUtils.FindClose(FS); //关闭查询
end;