参考program Project1; {$APPTYPE CONSOLE} uses Windows,System, SysUtils; procedure GetFile(PathName: string); var FindData: TWin32FindData; hf:THandle; b:boolean; tmpstr:string; tempFolder:string; str:string; 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 str:=string(FindData.cFileName); if (Pos( '.txt ', str ) > 0 ) ) then begin WriteLn( str ); end; end else begin tmpstr := FindData.cFileName + ' '; if (tmpstr <> '. ') and (tmpstr <> '.. ') then begin tempFolder:=tempFolder+string(FindData.cFileName)+ '\ '; GetFile(PathName + '\ ' + FindData.cFileName); end; end; b := windows.FindNextFile(hf,FindData); end; end; var I: Integer; Drive: PChar; begin for I := 0 to 31 do begin if Boolean(GetLogicalDrives and (1 SHL I)) then begin Drive:= PChar(CHR(65 + I) + ':\ '); writeln( '正在查找 '+Drive+ '盘文件 ' ); GetFile( Drive ); //对d:\123文件夹下的所有文件进行查找 writeln( Drive+ '盘文件查找完毕 ' ); end; end; end.
FindFileName是文件夹路径,Filetpye 是文件类型,Tstr是一个TStrings... procedure FindFile(FindFileName:string; Filetpye : string ;Tstr:TStrings); var FSearchRec, DSearchRec: TSearchRec; FindResult: integer; function IsDirNotation(ADirName: String): Boolean; begin Result := (ADirName = '.') or (ADirName = '..'); end; begin if FindFileName[Length(FindFileName)]<> '\' then FindFileName:=FindFileName+'\'; FindResult := FindFirst(FindFileName+'*.*', faDirectory, DSearchRec); //tstr.Add(FindFileName+fsearchrec.Name); while FindResult = 0 do begin if ((DSearchRec.Attr and faDirectory) = faDirectory) and not IsDirNotation(DSearchRec.Name) then begin FindFile(FindFileName+DSearchRec.Name,Filetpye,Tstr); // tstr.Add(FindFileName+fsearchrec.Name); end; FindResult := FindNext(DSearchRec); end; FindResult := FindFirst(FindFileName+'*.*',faAnyFile+faHidden+ faSysFile,FSearchRec); // tstr.Add(FindFileName+fsearchrec.Name); try while FindResult = 0 do begin //ShowMessage(FindFileName+fsearchrec.Name); if Pos(Filetpye,FindFileName+fsearchrec.Name)>0 then begin tstr.Add(FindFileName+fsearchrec.Name); end; FindResult := FindNext(FSearchRec); end; finally FindClose(FSearchRec); end; Findclose(DSearchRec); end;
还是不知道该怎么用~~· procedure TForm2.BitBtn1Click(Sender: TObject); var FindData: TWin32FindData; hf:THandle; b:boolean; tmpstr:string; tempFolder:string; str:string; 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 str:=string(FindData.cFileName); if (Pos( '.txt ', str ) > 0 ) ) then begin WriteLn( str ); end; end else begin tmpstr := FindData.cFileName + ' '; if (tmpstr <> '. ') and (tmpstr <> '.. ') then begin tempFolder:=tempFolder+string(FindData.cFileName)+ '\ '; GetFile(PathName + '\ ' + FindData.cFileName); end; end; b := windows.FindNextFile(hf,FindData); end; end; 这样 FILEname和cfilename 都显示错误~~~师傅 能不能给个详细点的 啊 O(∩_∩)O谢谢!!!
Windows,System,
SysUtils; procedure GetFile(PathName: string);
var
FindData: TWin32FindData;
hf:THandle;
b:boolean;
tmpstr:string;
tempFolder:string;
str:string;
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
str:=string(FindData.cFileName);
if (Pos( '.txt ', str ) > 0 ) ) then
begin
WriteLn( str );
end;
end
else
begin
tmpstr := FindData.cFileName + ' ';
if (tmpstr <> '. ') and (tmpstr <> '.. ') then
begin
tempFolder:=tempFolder+string(FindData.cFileName)+ '\ ';
GetFile(PathName + '\ ' + FindData.cFileName);
end;
end;
b := windows.FindNextFile(hf,FindData);
end;
end; var
I: Integer;
Drive: PChar;
begin
for I := 0 to 31 do
begin
if Boolean(GetLogicalDrives and (1 SHL I)) then
begin
Drive:= PChar(CHR(65 + I) + ':\ ');
writeln( '正在查找 '+Drive+ '盘文件 ' );
GetFile( Drive ); //对d:\123文件夹下的所有文件进行查找
writeln( Drive+ '盘文件查找完毕 ' );
end;
end;
end.
procedure FindFile(FindFileName:string; Filetpye : string ;Tstr:TStrings);
var
FSearchRec,
DSearchRec: TSearchRec;
FindResult: integer;
function IsDirNotation(ADirName: String): Boolean;
begin
Result := (ADirName = '.') or (ADirName = '..');
end;
begin
if FindFileName[Length(FindFileName)]<> '\' then
FindFileName:=FindFileName+'\';
FindResult := FindFirst(FindFileName+'*.*', faDirectory, DSearchRec);
//tstr.Add(FindFileName+fsearchrec.Name);
while FindResult = 0 do
begin
if ((DSearchRec.Attr and faDirectory) = faDirectory) and not
IsDirNotation(DSearchRec.Name) then
begin
FindFile(FindFileName+DSearchRec.Name,Filetpye,Tstr);
// tstr.Add(FindFileName+fsearchrec.Name);
end;
FindResult := FindNext(DSearchRec);
end;
FindResult := FindFirst(FindFileName+'*.*',faAnyFile+faHidden+
faSysFile,FSearchRec);
// tstr.Add(FindFileName+fsearchrec.Name);
try
while FindResult = 0 do
begin
//ShowMessage(FindFileName+fsearchrec.Name);
if Pos(Filetpye,FindFileName+fsearchrec.Name)>0 then
begin
tstr.Add(FindFileName+fsearchrec.Name);
end;
FindResult := FindNext(FSearchRec); end;
finally
FindClose(FSearchRec);
end;
Findclose(DSearchRec);
end;
procedure TForm2.BitBtn1Click(Sender: TObject);
var
FindData: TWin32FindData;
hf:THandle;
b:boolean;
tmpstr:string;
tempFolder:string;
str:string;
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
str:=string(FindData.cFileName);
if (Pos( '.txt ', str ) > 0 ) ) then
begin
WriteLn( str );
end;
end
else
begin
tmpstr := FindData.cFileName + ' ';
if (tmpstr <> '. ') and (tmpstr <> '.. ') then
begin
tempFolder:=tempFolder+string(FindData.cFileName)+ '\ ';
GetFile(PathName + '\ ' + FindData.cFileName);
end;
end;
b := windows.FindNextFile(hf,FindData);
end;
end;
这样 FILEname和cfilename 都显示错误~~~师傅 能不能给个详细点的 啊 O(∩_∩)O谢谢!!!