下面的方法可以解决你的问题,当然递归的时候传入的参数就是目录,否则就是文件了。 只是你的改造一下。procedure TMainForm.FindFiles(APath: String); var FSearchRec, DSearchRec: TSearchRec; FindResult: integer; // fData:WIN32_FIND_DATA; function IsDirNotation(ADirName: String): Boolean; begin Result := (ADirName = '.') or (ADirName = '..'); end;begin APath := GetDirectoryName(APath); FindResult := FindFirst(APath+FFileName,faAnyFile+faHidden+ faSysFile+faReadOnly,FSearchRec); try while FindResult = 0 do begin ExtractFileExt(StrPas(FSearchRec.FindData.cFileName)); lbFiles.Items.Add(LowerCase(APath+FSearchRec.Name)); FindInfoInfiles(APath+FSearchRec.Name,edtFileInfo.Text); FindResult := FindNext(FSearchRec); end; FindResult := FindFirst(APath+'*.*', faDirectory, DSearchRec); while FindResult = 0 do begin if ((DSearchRec.Attr and faDirectory) = faDirectory) and not IsDirNotation(DSearchRec.Name) then begin FindFiles(APath+DSearchRec.Name); // Recursion here end; FindResult := FindNext(DSearchRec); end; finally FindClose(FSearchRec); end; end;
FindFirst的example帮助,很清楚的
FindFirst, FindNext, FindClose ExampleThe following example uses an edit control, a button, a string grid, and seven check boxes. The check boxes correspond to the seven possible file attributes. When the button is clicked, the path specified in the edit control is searched for files matching the checked file attributes. The names and sizes of the matching files are inserted into the string grid.procedure TForm1.Button1Click(Sender: TObject);var sr: TSearchRec; FileAttrs: Integer; begin StringGrid1.RowCount := 1; if CheckBox1.Checked then FileAttrs := faReadOnly else FileAttrs := 0; if CheckBox2.Checked then FileAttrs := FileAttrs + faHidden; if CheckBox3.Checked then FileAttrs := FileAttrs + faSysFile; if CheckBox4.Checked then FileAttrs := FileAttrs + faVolumeID; if CheckBox5.Checked then FileAttrs := FileAttrs + faDirectory; if CheckBox6.Checked then FileAttrs := FileAttrs + faArchive; if CheckBox7.Checked then FileAttrs := FileAttrs + faAnyFile; with StringGrid1 do begin RowCount := 0; if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then begin repeat if (sr.Attr and FileAttrs) = sr.Attr then begin RowCount := RowCount + 1; Cells[1,RowCount-1] := sr.Name; Cells[2,RowCount-1] := IntToStr(sr.Size); end; until FindNext(sr) <> 0; FindClose(sr); end; end; end;
if ((DSearchRec.Attr and faDirectory) = faDirectory) 这就是判断是否为目录的意思吧.,呵呵.谢谢了.问题已经解决了.我就是找了半天了.谢谢哈...
只是你的改造一下。procedure TMainForm.FindFiles(APath: String);
var
FSearchRec,
DSearchRec: TSearchRec;
FindResult: integer;
// fData:WIN32_FIND_DATA;
function IsDirNotation(ADirName: String): Boolean;
begin
Result := (ADirName = '.') or (ADirName = '..');
end;begin
APath := GetDirectoryName(APath);
FindResult := FindFirst(APath+FFileName,faAnyFile+faHidden+
faSysFile+faReadOnly,FSearchRec);
try
while FindResult = 0 do
begin
ExtractFileExt(StrPas(FSearchRec.FindData.cFileName));
lbFiles.Items.Add(LowerCase(APath+FSearchRec.Name));
FindInfoInfiles(APath+FSearchRec.Name,edtFileInfo.Text);
FindResult := FindNext(FSearchRec);
end; FindResult := FindFirst(APath+'*.*', faDirectory, DSearchRec);
while FindResult = 0 do
begin
if ((DSearchRec.Attr and faDirectory) = faDirectory)
and not IsDirNotation(DSearchRec.Name) then
begin
FindFiles(APath+DSearchRec.Name); // Recursion here
end;
FindResult := FindNext(DSearchRec);
end;
finally
FindClose(FSearchRec);
end;
end;
sr: TSearchRec;
FileAttrs: Integer;
begin
StringGrid1.RowCount := 1;
if CheckBox1.Checked then
FileAttrs := faReadOnly
else
FileAttrs := 0;
if CheckBox2.Checked then
FileAttrs := FileAttrs + faHidden;
if CheckBox3.Checked then
FileAttrs := FileAttrs + faSysFile;
if CheckBox4.Checked then
FileAttrs := FileAttrs + faVolumeID;
if CheckBox5.Checked then FileAttrs := FileAttrs + faDirectory;
if CheckBox6.Checked then
FileAttrs := FileAttrs + faArchive;
if CheckBox7.Checked then FileAttrs := FileAttrs + faAnyFile; with StringGrid1 do
begin
RowCount := 0; if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then begin
repeat
if (sr.Attr and FileAttrs) = sr.Attr then
begin
RowCount := RowCount + 1;
Cells[1,RowCount-1] := sr.Name;
Cells[2,RowCount-1] := IntToStr(sr.Size);
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
end;
这就是判断是否为目录的意思吧.,呵呵.谢谢了.问题已经解决了.我就是找了半天了.谢谢哈...