查找文件问题,为什么用FindFirst和FindNext无效啊,请高手帮帮忙(50分)

  • 主题发起人 主题发起人 chinaxuguojun
  • 开始时间 开始时间
C

chinaxuguojun

Unregistered / Unconfirmed
GUEST, unregistred user!
代码很简单,就是在主窗上放上一个 Button1 然后写上事件,就可以了,但就是调用不出来啊
怪怪的

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FStringList: TStringList;
procedure FileExeFile(Path: string;FileType: Integer);
public

end;

var
Form1: TForm1;

implementation

uses FileCtrl,StrUtils;

{$R *.dfm}



procedure TForm1.Button1Click(Sender: TObject);
var
path: string;
FileAttrs: Integer;
begin
FileAttrs := faAnyFile;//faReadOnly + faHidden + faSysFile + faDirectory + faArchive;
SelectDirectory('','',path);
if path <> '' then
begin
FileExeFile(path,FileAttrs)
end else
begin
showmessage('请选择路径!');
end;
end;

procedure TForm1.FileExeFile(Path: string;FileType: Integer);
var
sr: TSearchRec;
begin
FStringList.Clear;
if FindFirst(Path,FileType, sr) = 0 then
begin
repeat
if (sr.Attr and FileType) = faDirectory then
FileExeFile(Path + '/' + Sr.Name,FileType);
if (sr.Attr and FileType) = sr.Attr then
begin
if UpperCase(RightStr(Sr.Name,4)) = '.EXE' then
FStringList.Add(Path + '/' + Sr.Name);
end;
until FindNext(sr) <> 0
FindClose(sr);
FStringList.SaveToFile('Result.txt');
end;


end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FStringList := TStringList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
FStringList.Free;
end;

end.
 
老大,代码在这里了,请你按需取用好不好:
procedure GetAllFileNames(SearchDir:Boolean;WatchSubTree:Boolean=False;FileNameExt:String='');
var SearchRec: TSearchRec;
TempList:TStrings;
P:PFileCollection;
i,iExt:Integer;
J:Boolean;
SL:TStringList;
ValidateDir:Boolean;
S:String;
begin
if FileList<>nil then begin
for i:=0 to FileList.Count-1 do begin
P:=PFileCollection(FileList);
Dispose(P);
end;
FreeAndNil(FileList);
end;
FileList:=TList.Create;
SL:=TStringList.Create;
if Trim(FileNameExt)='' then exit;
splite(UpperCase(Trim(FileNameExt)),',',SL);
for iExt:=0 to SL.Count-1 do begin
for i:=0 to DirectList.Count-1 do begin
if not SearchDir then ValidateDir:=True
else begin
if Uppercase(Trim(GetFactPath(DirectList)))=
Uppercase(Trim(GetFactPath(SysDateDir))) then ValidateDir:=True
else ValidateDir:=False;
end;
if ValidateDir then begin
S:=GetFactPath(DirectList)+'*.'+Trim(SL[iExt]);
if FindFirst(S,faArchive,SearchRec)=0 then begin
J:=True;
New(P);
P.FileName:=SearchRec.Name;
P.DirPath:=DirectList;
P.FileSize:=SearchRec.Size;
P.ModifyTime:=FileDateToDateTime(SearchRec.Time);
FileList.Add(P);
while FindNext(SearchRec)=0 do begin
New(P);
P.FileName:=SearchRec.Name;
P.DirPath:=DirectList;
P.FileSize:=SearchRec.Size;
P.ModifyTime:=FileDateToDateTime(SearchRec.Time);
FileList.Add(P);
end;
end;
end;
end;
end;end;
 
path需要文件的扩展名什么的,如(*.txt,*.*,*.exe)
if FindFirst(Path,FileType, sr) = 0 then
 
但我还想查找下面的子目录呢
 
再在下面用
子目录 + *.*
 
谢各位了,我是在path中没有文件的扩展名
 
目录有什么扩展名没有,我只想查某一目录下的子目录和某类型的文件,但如果要查目录的话,好像只有用*.*去查,这样就查询的效率不高了
 
一样的效率三,只是最后要判断一下是不是目录而已,你查其他文件还不是要判断一下
 
后退
顶部