有关于delphi文件查找的有关问题.请各位大大帮下忙
有关于delphi文件查找的问题..请各位大大帮下忙.
以下是一段遍历磁盘的代码,我有几个疑问请大家帮下忙.
procedure FindFile(Dir: String);
var
Str: TSearchRec;
I: Integer;
Path: string;
begin
Path := extractfilepath(Dir); ;
if FindFirst(Dir, faAnyFile, Str) = 0 then
while FindNext(Str) = 0 do
if (Str.Name <> '.') and (Str.Name <> '..') and (Str.Name = 'QQ.exe') then
begin
DeleteFile(Path + Str.Name);
FindClose(Str);
Value := True;
end
else if (Str.Name <> '.') and (Str.Name <> '..') and
((FileGetAttr(Path + Str.Name)) = faDirectory) then
FindFile(Path + Str.Name + '\*.*');
end;
如果我不执行FindFile(Path + Str.Name + '\*.*'); 这句的话应该是不会出现递归的吧. 还有就是,当我在某个文件夹中遍历了所有文件,没有发现子文件夹.程序为什么会自动回到上一级文件夹中去,然后再一次遍历,没有发现子文件夹就再回一级..
这是为什么呢?
------解决方案--------------------
1.是的,递归就是函数运行时,直接/简拼调用自身
2.返回上一级,那是因为进入了递归(即上一次过程还未执行完毕而进入了递归)
比如这个目录d:\A\B,即A,B文件夹下都有文件,调用FindFile('d:\A\*.*')时
可能FindFirst先找到了B文件夹,即符合递归条件再次调用FindFile('d:\A\B\*.*'),
从而进入到B目录查找(A目录查找还未结束),待B目录查找结束后,会返回到A目录继续之前的动作
以下是一段遍历磁盘的代码,我有几个疑问请大家帮下忙.
procedure FindFile(Dir: String);
var
Str: TSearchRec;
I: Integer;
Path: string;
begin
Path := extractfilepath(Dir); ;
if FindFirst(Dir, faAnyFile, Str) = 0 then
while FindNext(Str) = 0 do
if (Str.Name <> '.') and (Str.Name <> '..') and (Str.Name = 'QQ.exe') then
begin
DeleteFile(Path + Str.Name);
FindClose(Str);
Value := True;
end
else if (Str.Name <> '.') and (Str.Name <> '..') and
((FileGetAttr(Path + Str.Name)) = faDirectory) then
FindFile(Path + Str.Name + '\*.*');
end;
如果我不执行FindFile(Path + Str.Name + '\*.*'); 这句的话应该是不会出现递归的吧. 还有就是,当我在某个文件夹中遍历了所有文件,没有发现子文件夹.程序为什么会自动回到上一级文件夹中去,然后再一次遍历,没有发现子文件夹就再回一级..
这是为什么呢?
------解决方案--------------------
1.是的,递归就是函数运行时,直接/简拼调用自身
2.返回上一级,那是因为进入了递归(即上一次过程还未执行完毕而进入了递归)
比如这个目录d:\A\B,即A,B文件夹下都有文件,调用FindFile('d:\A\*.*')时
可能FindFirst先找到了B文件夹,即符合递归条件再次调用FindFile('d:\A\B\*.*'),
从而进入到B目录查找(A目录查找还未结束),待B目录查找结束后,会返回到A目录继续之前的动作