怎样用findfirst 和findnext计算文件数目

LinLinLin 2004-09-28 03:31:51
怎样算出某一文件夹下面的所有文件夹和文件的数目?
(用findfirst 和findnext计算数目)
...全文
211 11 打赏 收藏 举报
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
skynew2004 2004-10-06
  • 打赏
  • 举报
回复
该结题了
zdq801104 2004-10-06
  • 打赏
  • 举报
回复
看看这个可能有用
unit mainunit;

interface

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

type
TForm1 = class(TForm)
Memo1: TMemo;
Label1: TLabel;
Label2: TLabel;
ComboBox1: TComboBox;
Edit1: TEdit;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
Label3: TLabel;
procedure FormCreate(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
private
{ Private declarations }
public
function CurrentIsValidDir(SearchRec:TSearchRec):integer;
procedure RecurSearchFile(CurrentDir:string;SearchFileType:string;SearchResult:TStrings;var Number:integer);
{ Public declarations }
end;

var
Form1: TForm1;
TotalFileNumbers:Integer;
SearchFileType:String;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
TotalFileNumbers:=0;
SearchFileType:='*.txt';
end;

function TForm1.CurrentIsValidDir(SearchRec:TSearchRec):integer;
begin
if ((SearchRec.Attr <> 16) and
(SearchRec.Name<>'.') and
(SearchRec.Name<>'..')) then
Result:=0
else if ((SearchRec.Attr = 16) and
(SearchRec.Name<>'.') and
(SearchRec.Name<>'..')) then
Result:=1
else
Result:=2;
end;

Procedure TForm1.RecurSearchFile(CurrentDir:string;SearchFileType:string;SearchResult:TStrings;var Number:integer);
var
i:integer;
Subdir:TStringList;
SearchRec:TsearchRec;
begin
//第一次调用FindFirst和FindNext查找符合要求的文件
if (FindFirst(CurrentDir+SearchFileType, faAnyFile, SearchRec)=0) then
begin
repeat
if CurrentIsValidDir(SearchRec)=0 then
begin
Inc(Number);
Searchresult.Add(CurrentDir+SearchRec.Name);
end;
application.ProcessMessages ;
until (FindNext(SearchRec) <> 0);
end;
FindClose(SearchRec);

//以下是递归部分,查找各子目录。
Subdir:=TStringList.Create;
if (FindFirst(CurrentDir+'*.*', faDirectory, SearchRec)=0) then
begin
repeat
if CurrentIsValidDir(SearchRec)=1 then
begin
Subdir.Add(SearchRec.Name);
end;
application.ProcessMessages ;
until (FindNext(SearchRec) <> 0);
end;
FindClose(SearchRec);
for i:=0 to Subdir.Count-1 do
begin
RecurSearchfile(CurrentDir+Subdir.Strings[i]+'\',SearchFileType,Searchresult,Number);
end;

//资源释放并返回结果。
Subdir.Free;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
Case ComboBox1.ItemIndex of
0:SearchFileType:='*.txt';
1:SearchFileType:='*.bmp';
2:SearchFileType:='*.mp3';
3:SearchFileType:='*.*';
end;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
if Edit1.Text='' then
BitBtn2.Enabled:=False
else
BitBtn2.Enabled:=True;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
SelectDir:string;
begin
if SelectDirectory(SelectDir, [sdAllowCreate, sdPerformCreate, sdPrompt],0) then
begin
if length(SelectDir) > 3 then
SelectDir:=SelectDir+'\';
Edit1.Text:=SelectDir;
end;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
memo1.lines.Clear ;
TotalFileNumbers:=0;
RecurSearchFile(Edit1.Text,SearchFileType,memo1.lines, TotalFileNumbers);
Label3.Caption:='查找结果显示:'+'一共在当前目录下找到'+IntToStr(TotalFileNumbers)+'个'+SearchFileType+'类型的文件';
end;

procedure TForm1.BitBtn3Click(Sender: TObject);
begin
Close;
end;

end.
skynew2004 2004-10-04
  • 打赏
  • 举报
回复
//用递归方法再打出子目录下的文件
count:=count+getfilecount(sourcepath+sr.Name);

即可。
skynew2004 2004-10-04
  • 打赏
  • 举报
回复
我看过你的程序了.
你不应改变我的程序代码,我又把它改回去了,这样就行了

请从以下地址上速下载我改过的。
http://upserver3.ys168.com/ys168up/D1/YY.aspx?f=04K49E0E4E1E2E3D5E0E1D6AVI7AVI5F8G0D8E2D9E3D6E1A24E4E3E2E4E7B8

或在skynew.ys168.com中查看。



skynew2004
skynew2004 2004-10-02
  • 打赏
  • 举报
回复
function tmyfun.getfilecount(sourcepath:string):integer;
var sr1:TsearchRec;
count:integer;
begin
//加/
count:=0;
sourcepath:=includetrailingbackslash(sourcepath);
if findfirst(sourcepath + '*.*',faAnyFile, sr1)=0 then
begin
repeat
if (sr1.Name<>'.') and (sr1.Name<>'..') then
begin
if sr1.Attr<>faDirectory then
begin
count:=count+1;
end else
begin
//用递归方法再打出子目录下的文件
// getfilecount(sourcepath+sr.Name);
end;
end;
until findnext(sr1)<>0;
end;
result:=count;
findclose(sr1);
end;
todouwang 2004-10-01
  • 打赏
  • 举报
回复
学习
yueyixing 2004-09-28
  • 打赏
  • 举报
回复
ProgressBar.Position := Round(已下载文件数 * 100 / 总文件数);
斐偲 2004-09-28
  • 打赏
  • 举报
回复
关注,顶一下!
LinLinLin 2004-09-28
  • 打赏
  • 举报
回复
上面的高手我这段代码应该怎么改才能让进度条一格一格显示,
我现在的思路是获取一个目录下了文件数目,然后把这个值付给infoFrm.ProgressBar1.Position.max
procedure pSearchFile(mPath: TFileName);
var
vSearchRec: TSearchRec;
K,i: Integer;
begin
K := FindFirst(mPath + '\*.*', faAnyFile, vSearchRec);
i:=i+1;
while K = 0 do
begin
if (vSearchRec.Attr and faDirectory > 0) and
(Pos(vSearchRec.Name, '..') = 0) then
pSearchFile(mPath + '\' + vSearchRec.Name)
else
if Pos(vSearchRec.Name, '..') = 0 then
pAppendFile(mPath + '\' + vSearchRec.Name);
infoFrm.ProgressBar1.Position := i;
K := FindNext(vSearchRec);
end;
FindClose(vSearchRec);
end; { pSearchFile }
yueyixing 2004-09-28
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
strFileName: string; // holds the name of the file to find
FindFileData: TWin32FindData; // holds file information
SearchHandle: THandle; // holds the search handle
begin
{clear any listed files}

ListView1.Items.Clear;

{if there was no file specified, then specify all files}
if Edit2.GetTextLen = 0 then Edit2.Text:= '*.*';

{construct the filename string}
strFileName:= DirectoryListBox2.Directory + '\' + Edit2.Text;

{set the directory to the specified directory}
SetCurrentDirectory(PChar(DirectoryListBox2.Directory));

{begin the search}
SearchHandle:=FindFirstFile(PChar(strFileName), FindFileData);

{continue searching for all matching files in the current directory}
if (SearchHandle <> INVALID_HANDLE_VALUE) then
repeat
ListView1.Items.Add.Caption:=FindFileData.cFileName;
until (FindNextFile(SearchHandle,FindFileData) = FALSE);

{all files have been found, so close the search handle}
Windows.FindClose(SearchHandle);
end;
yar1984 2004-09-28
  • 打赏
  • 举报
回复
var
SearchRec: TSearchRec;
Name,Name1:String;
i:integer;
begin
Name:=copy(dbColorCard.Text,1,3);
Name1:=Name+'*.*';
if FindFirst(imgPath +Name1, faAnyFile, SearchRec) = 0 then
repeat
i:=i+1;
until (FindNext(SearchRec) <> 0);
FindClose(SearchRec);
end;
imgpath:是一个文件夹的路径你可以输入你要查找的文件夹的路径;
end;
发帖
Windows SDK/API

1177

社区成员

Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
帖子事件
创建了帖子
2004-09-28 03:31
社区公告
暂无公告