procedure TForm1.FormCreate(Sender: TObject);
var
path:String;
i:integer;
s:string;
begin
if hThreadHandle1 = 0 then
begin
for i:=0 to 25 do
begin
s:=String(char(65+i))+':';
if GetDriveType(pchar(s))=DRIVE_FIXED then
path:=(s);调用path搜索全盘gif时 , 只搜索最后一个分区,就不在搜索其他分区了.有什么解决方法?
可搜索完最后一个分区后再搜索到第二个 第三个 循环. ?
var
path:String;
i:integer;
s:string;
begin
if hThreadHandle1 = 0 then
begin
for i:=0 to 25 do
begin
s:=String(char(65+i))+':';
if GetDriveType(pchar(s))=DRIVE_FIXED then
path:=(s);调用path搜索全盘gif时 , 只搜索最后一个分区,就不在搜索其他分区了.有什么解决方法?
可搜索完最后一个分区后再搜索到第二个 第三个 循环. ?
var
searchRec:TSearchRec;
found:Integer;
tmpStr:String;
Extension,Fname:string;
begin
Result:=0; //查找结果(文件数)
//加上搜索后缀,得到类似'c:\*.*' 、'c:\windows\*.*'的搜索路径
tmpStr:=StrPas(path)+'\*.gif';
//在当前目录查找第一个文件、子目录
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控件
Extension:=UPPERCASE(ExtractFileExt(StrPas(path)+'\'+searchRec.Name));
Fname:= UPPERCASE(searchRec.Name);
filename:= StrPas(path)+'\'+searchRec.Name;
Form1.Memo1.Lines.Add(filename);
end;
//查找下一个文件或目录
found:=FindNext(searchRec);
end;
//释放资源
FindClose(searchRec);
end;procedure TForm1.FormCreate(Sender: TObject);
var
dwThreadID:Cardinal;
path:String;
i:integer;
s:string;
begin
if hThreadHandle1 = 0 then
begin
for i:=0 to 25 do
begin
s:=String(char(65+i))+':';
if GetDriveType(pchar(s))=DRIVE_FIXED then
path:=(s);
end;
Memo1.Lines.Clear;
//得到驱动盘的路径,这样可能啰嗦了些.
//创建线程,使用递归方法遍历文件
hThreadHandle1:=CreateThread(nil,0,@EnumFileInRecursion,StrNew(PChar(path)),0,dwThreadID);
{@EnumFileInRecursion是函数的地址
PChar('c:')是函数的参数,以指针传递
不能直接使用@EnumFileInRecursion('c:')}
end else
begin
TerminateThread(hThreadHandle1,0);
CloseHandle(hThreadHandle1);
hThreadHandle1 := 0;
end;
end;
var
dwThreadID:Cardinal;
path:String;
i:integer;
s:string;
begin
if hThreadHandle1 = 0 then
begin
for i:=0 to 25 do
begin
s:=String(char(65+i))+ ': ';
if GetDriveType(pchar(s))=DRIVE_FIXED then
begin
path:=(s);
Break; //------------------------------------------关键
end;
end;
Memo1.Lines.Clear;
//得到驱动盘的路径,这样可能啰嗦了些.
//创建线程,使用递归方法遍历文件
hThreadHandle1:=CreateThread(nil,0,@EnumFileInRecursion,StrNew(PChar(path)),0,dwThreadID);
{@EnumFileInRecursion是函数的地址
PChar( 'c: ')是函数的参数,以指针传递
不能直接使用@EnumFileInRecursion( 'c: ')}
end else
begin
TerminateThread(hThreadHandle1,0);
CloseHandle(hThreadHandle1);
hThreadHandle1 := 0;
end;
end;
var
dwThreadID:Cardinal;
path:String;
i:integer;
s:string;
begin
if hThreadHandle1 = 0 then
begin
for i:=0 to 25 do
begin
s:=String(char(65+i))+ ': ';
if GetDriveType(pchar(s))=DRIVE_FIXED then
begin
path:=(s);
Break;
Memo1.Lines.Clear;
//得到驱动盘的路径,这样可能啰嗦了些.
//创建线程,使用递归方法遍历文件
hThreadHandle1:=CreateThread(nil,0,@EnumFileInRecursion,StrNew(PChar(path)),0,dwThreadID);
{@EnumFileInRecursion是函数的地址
PChar( 'c: ')是函数的参数,以指针传递
不能直接使用@EnumFileInRecursion( 'c: ')}
end;
end;
end else
begin
TerminateThread(hThreadHandle1,0);
CloseHandle(hThreadHandle1);
hThreadHandle1 := 0;
end;
end;
for i:=0 to 25 do
begin
s:=String(char(65+i))+ ': ';
if GetDriveType(pchar(s))=DRIVE_FIXED then path:=(s);
end; 明显循环出错!你应该把
hThreadHandle1:=CreateThread(nil,0,@EnumFileInRecursion,StrNew(PChar(path)),0,dwThreadID);
放入循环体
var
dwThreadID:Cardinal;
path:String;
i:integer;
s:string;
begin
if hThreadHandle1 = 0 then
begin
for i:=0 to 25 do
begin
s:=String(char(65+i))+ ': ';
if GetDriveType(pchar(s))=DRIVE_FIXED then
begin
path:=(s);
Break;
Memo1.Lines.Clear;
//得到驱动盘的路径,这样可能啰嗦了些.
//创建线程,使用递归方法遍历文件
hThreadHandle1:=CreateThread(nil,0,@EnumFileInRecursion,StrNew(PChar(path)),0,dwThreadID);
{@EnumFileInRecursion是函数的地址
PChar( 'c: ')是函数的参数,以指针传递
不能直接使用@EnumFileInRecursion( 'c: ')}
end;
end;
end else
begin
TerminateThread(hThreadHandle1,0);
CloseHandle(hThreadHandle1);
hThreadHandle1 := 0;
end;
end;这个你没试吗?函数EnumFileInRecursion不用改
var
dwThreadID:Cardinal;
path:String;
i:integer;
s:string;
begin
if hThreadHandle1 = 0 then
begin
for i:=0 to 25 do
begin
s:=String(char(65+i))+ ': ';
if GetDriveType(pchar(s))=DRIVE_FIXED then
begin
path:=(s);
Memo1.Lines.Clear;
//得到驱动盘的路径,这样可能啰嗦了些.
//创建线程,使用递归方法遍历文件
hThreadHandle1:=CreateThread(nil,0,@EnumFileInRecursion,StrNew(PChar(path)),0,dwThreadID);
{@EnumFileInRecursion是函数的地址
PChar( 'c: ')是函数的参数,以指针传递
不能直接使用@EnumFileInRecursion( 'c: ')}
end;
end;
end else
begin
TerminateThread(hThreadHandle1,0);
CloseHandle(hThreadHandle1);
hThreadHandle1 := 0;
end;
end;这个,我把break删掉了
反正我用你给的什么也搜索不到,是不是不全阿
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Contnrs, ExtCtrls, FileCtrl;type
TForm1 = class(TForm)
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
hThreadHandle1:THandle;
hThreadHandle2:THandle;
public
{ Public declarations }
end;var
Form1: TForm1;
filename: string;implementation{$R *.dfm}{用递归的方法遍历文件}function EnumFileInRecursion(path:PChar):Longint;stdcall;
var
searchRec:TSearchRec;
found:Integer;
tmpStr:String;
Extension,Fname: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控件
Extension:=UPPERCASE(ExtractFileExt(StrPas(path)+'\'+searchRec.Name));
Fname:= UPPERCASE(searchRec.Name);
filename:= StrPas(path)+'\'+searchRec.Name;
Form1.Memo1.Lines.Add(filename);
end;
//查找下一个文件或目录
found:=FindNext(searchRec);
end;
//释放资源
FindClose(searchRec);
end;procedure TForm1.FormCreate(Sender: TObject);
var
dwThreadID:Cardinal;
path:String;
i:integer;
s:string;
begin
if hThreadHandle1 = 0 then
begin
for i:=0 to 25 do
begin
s:=String(char(65+i))+':';
if GetDriveType(pchar(s))=DRIVE_FIXED then
path:=(s);
end;
Memo1.Lines.Clear;
hThreadHandle1:=CreateThread(nil,0,@EnumFileInRecursion,StrNew(PChar(path)),0,dwThreadID);
//得到驱动盘的路径,这样可能啰嗦了些.
//创建线程,使用递归方法遍历文件
{@EnumFileInRecursion是函数的地址
PChar('c:')是函数的参数,以指针传递
不能直接使用@EnumFileInRecursion('c:')}
end else
begin
TerminateThread(hThreadHandle1,0);
CloseHandle(hThreadHandle1);
hThreadHandle1 := 0;
end;
end;end.我这边只能搜索最后一个盘? 你QQ 方便告诉不?
begin
s:=String(char(65+i))+ ': ';
if GetDriveType(pchar(s))=DRIVE_FIXED then
path:=(s);
end;
这段执行的结果只有一个,就是得到你的最后一个本地物理驱动器,错误应该就在这里
我在单位,机器里一大堆木马和病毒,不敢上QQ
你的程序其实问题不大,多是逻辑上的
多跟踪多调试很容易发现问题
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Contnrs, ExtCtrls, FileCtrl;type
TForm1 = class(TForm)
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
hThreadHandle1:THandle;
hThreadHandle2:THandle;
public
{ Public declarations }
end;var
Form1: TForm1;
filename: string;implementation{$R *.dfm}{用递归的方法遍历文件}function EnumFileInRecursion(path:PChar):Longint;stdcall;
var
searchRec:TSearchRec;
found:Integer;
tmpStr:String;
Extension,Fname: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控件
Extension:=UPPERCASE(ExtractFileExt(StrPas(path)+ '\'+searchRec.Name));
Fname:= UPPERCASE(searchRec.Name);
filename:= StrPas(path)+ '\'+searchRec.Name;
Form1.Memo1.Lines.Add(filename);
end;
//查找下一个文件或目录
found:=FindNext(searchRec);
end;
//释放资源
FindClose(searchRec);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
dwThreadID:Cardinal;
path:String;
i:integer;
s:string;
begin
if hThreadHandle1 = 0 then
begin
for i:=0 to 25 do
begin
s:=String(char(65+i))+ ':';
if GetDriveType(pchar(s))=DRIVE_FIXED then
begin
path:=(s);
Memo1.Lines.Clear;
hThreadHandle1:=CreateThread(nil,0,@EnumFileInRecursion,StrNew(PChar(path)),0,dwThreadID);
//得到驱动盘的路径,这样可能啰嗦了些.
//创建线程,使用递归方法遍历文件
{@EnumFileInRecursion是函数的地址
PChar( 'c: ')是函数的参数,以指针传递
不能直接使用@EnumFileInRecursion( 'c: ')}
end;
end;
end else
begin
TerminateThread(hThreadHandle1,0);
CloseHandle(hThreadHandle1);
hThreadHandle1 := 0;
end;
end;end.
可以了,但我执行一会就出错了,可能是内存溢出