首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

请问个遍历函数有关问题

2012-02-05 
请教个遍历函数问题在网上看到了个函数{用递归的方法遍历文件}functionEnumFileInRecursion(path:PChar):L

请教个遍历函数问题
在网上看到了个函数
{用递归的方法遍历文件}
function   EnumFileInRecursion(path:PChar):Longint;stdcall;
var
        searchRec:TSearchRec;
        found:Integer;
        tmpStr:String;
begin
        Result:=0;   //查找结果(文件数)
        //加上搜索后缀,得到类似 'c:\*.* '   、 'c:\windows\*.* '的搜索路径
        tmpStr:=StrPas(path)+ '\*.* ';
        //在当前目录查找第一个文件、子目录
        found:=FindFirst(tmpStr,faAnyFile,searchRec);
        while   found=0   do
        //找到了一个文件或目录后
        begin
                //如果找到的是个目录
                if   (searchRec.Attr   and   faDirectory) <> 0   then
                begin
                        {在搜索非根目录(C:\、D:\)下的子目录时会出现 '. ', '.. '的 "虚拟目录 "
                        大概是表示上层目录和下层目录吧。。。要过滤掉才可以}
                        if   (searchRec.Name   <>   '. ')   and   (searchRec.Name   <>   '.. ')   then
                        begin
                                {由于查找到的子目录只有个目录名,所以要添上上层目录的路径
                                searchRec.Name   =   'Windows ';tmpStr:= 'c:\Windows ';
                                加个断点就一清二楚了}
                                tmpStr:=StrPas(path)+ '\ '+searchRec.Name;
                                //自身调用,查找子目录,递归。。。。
                                Result:=Result+EnumFileInRecursion(PChar(tmpStr));
                        end;
                end
                //如果找到的是个文件
                {这个也是递归的结束条件,结束条件对于理解递归来说,相当重要}
                else   begin
                        {Result记录着搜索到的文件数。可是我是用CreateThread创建线程
                        来调用函数的,不知道怎么得到这个返回值。。。我不想用全局变量}
                        Result:=Result+1;
                        //把找到的文件加到Memo控件
                        Form1.Memo1.Lines.Add(StrPas(path)+ '\ '+searchRec.Name);
                  end;


                  //查找下一个文件或目录
                found:=FindNext(searchRec);
        end;
        //释放资源
        FindClose(searchRec);
end;
只用到了windows,sysutils两个单元,想利用这个函数写个删除硬盘里文件名为setup.exe   的程序,运行后遍历所有驱动器,找的文件名为setup.exe   就删掉。请大侠们提供下完整代码!

[解决办法]
procedure DeleteFile(pathname: string);
var
FindData: TWin32FindData;
hf:THandle;
b:boolean;
tmpstr:string;
procedure delete(FileName:string);
var
OpStruc:TSHFileOpStruct;
begin
with OpStruc do
begin
Wnd:=0;
wFunc:=FO_DELETE;
pFrom:=Pchar(FileName);
fFlags:=FOF_ALLOWUNDO;
end;
SHFileOperation(OpStruc);
end;

begin
hf := Windows.FindFirstFile(PChar(pathname + '\*.* '), FindData);
if hf = INVALID_HANDLE_VALUE then exit;
b := true;
while b do
begin
if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
begin
if uppercase(string(FindData.cFileName))=uppercase( 'setup.exe ') then
begin
delete( pathname + '\ ' + FindData.cFileName );
end;
end
else
begin
tmpstr := FindData.cFileName + ' ';
if (tmpstr <> '. ') and (tmpstr <> '.. ') then
begin
tempFolder:=tempFolder+string(FindData.cFileName)+ '\ ';
DeleteFile(pathname + '\ ' + FindData.cFileName);
end;
end;
b := windows.FindNextFile(hf,FindData);
end;
end;

热点排行