function GetFileCount(srcPath, srcFileName: string): Integer; var FileRec: TSearchrec; currPath: string; begin if srcPath[Length(srcPath)] <> '\' then srcPath := srcPath + '\'; currPath := srcPath + '*.*'; Result := 0; if FindFirst(currPath, faAnyFile, FileRec) = 0 then repeat if ((FileRec.Attr and faDirectory) <> 0) and (FileRec.Name <> '.') and (FileRec.Name <> '..') then begin Result := Result + GetFileCount(srcPath + FileRec.Name, srcFileName); end else if AnsiCompareText(srcFileName, FileRec.Name) = 0 then Result := Result + 1; until FindNext(FileRec) <> 0; end; 参数1:搜索路径 参数2:目标文件 返回值:文件个数 例如搜索C盘下所有文件名为“a.txt”的文件个数,GetFileCount('C:\','a.txt'); 你试试,应该可以,这个包括子目录,系统文件夹等。
给你贴出上面第二种情况的代码,自己研究吧,这种做法基本没听说有人这样做过//Interface 处定义结构体 TFileRecord = record sFileName: string; iCount: Integer; end; PFileRecord = ^TFileRecord; //implementation处定义全局变量 slFileResult: TStringList ;//该函数用于搜索指定路径文件,并将结果存之slFileResult中 function TForm1.GetFileCount(srcPath: string): Integer; var FileRec: TSearchrec; currPath: string; pFR: PFileRecord; begin if srcPath[Length(srcPath)] <> '\' then srcPath := srcPath + '\'; currPath := srcPath + '*.*'; Result := 0; if FindFirst(currPath, faAnyFile, FileRec) = 0 then repeat if (FileRec.Name = '.') or (FileRec.Name = '..') then Continue; if ((FileRec.Attr and faDirectory) <> 0) and (FileRec.Name <> '.') and (FileRec.Name <> '..') then begin Result := Result + GetFileCount(srcPath + FileRec.Name); end else if slFileResult.IndexOf(FileRec.Name) > -1 then begin pFR := PFileRecord(slFileResult.Objects[slFileResult.IndexOf(FileRec.Name)]); pFR.iCount := pFR.iCount + 1; end else if slFileResult.IndexOf(FileRec.Name) = -1 then begin New(pFR); pFR.sFileName := FileRec.Name; pFR.iCount := 1; slFileResult.AddObject(FileRec.Name, TObject(pFR)); end; until FindNext(FileRec) <> 0; end; //该处代码用于求出指定路径下具有相同文件名的文件个数 procedure TForm1.Button1Click(Sender: TObject); var i: Integer; pFR: PFileRecord; iCount: Integer; begin if slFileResult = nil then slFileResult := TStringList.Create; Caption := IntToStr(GetFileCount(Edit1.Text)); for i := 0 to slFileResult.Count - 1 do begin pFR := PFileRecord(slFileResult.Objects[i]); if pFR.iCount > 1 then begin iCount := iCount + 1; Memo1.Lines.Add(Format('%d----%s', [pFR.iCount, pFR.sFilename])); end; end; Caption := IntToStr(iCount); end;本来想在一个函数内全部搞定的,但是涉及到递归函数调用,一个函数不能实现 看看是不是你要的结果,当然如果想求第一种输出的话,修改最后一个函数就可以了
不是输入文件名,找它的同名文件? 分2步: 1、findfirst/findnext递归找出所有文件名到数据库 2、select filename.count(*) fc from t group by filename
var
FileRec: TSearchrec;
currPath: string;
begin
if srcPath[Length(srcPath)] <> '\' then srcPath := srcPath + '\';
currPath := srcPath + '*.*';
Result := 0;
if FindFirst(currPath, faAnyFile, FileRec) = 0 then
repeat
if ((FileRec.Attr and faDirectory) <> 0) and
(FileRec.Name <> '.') and
(FileRec.Name <> '..') then
begin
Result := Result + GetFileCount(srcPath + FileRec.Name, srcFileName);
end else
if AnsiCompareText(srcFileName, FileRec.Name) = 0 then
Result := Result + 1;
until FindNext(FileRec) <> 0;
end;
参数1:搜索路径
参数2:目标文件
返回值:文件个数
例如搜索C盘下所有文件名为“a.txt”的文件个数,GetFileCount('C:\','a.txt');
你试试,应该可以,这个包括子目录,系统文件夹等。
1.输出x+y?
2.输出2?
你要求的是哪种输出?
TFileRecord = record
sFileName: string;
iCount: Integer;
end;
PFileRecord = ^TFileRecord;
//implementation处定义全局变量
slFileResult: TStringList
;//该函数用于搜索指定路径文件,并将结果存之slFileResult中
function TForm1.GetFileCount(srcPath: string): Integer;
var
FileRec: TSearchrec;
currPath: string;
pFR: PFileRecord;
begin
if srcPath[Length(srcPath)] <> '\' then srcPath := srcPath + '\';
currPath := srcPath + '*.*';
Result := 0;
if FindFirst(currPath, faAnyFile, FileRec) = 0 then
repeat
if (FileRec.Name = '.') or (FileRec.Name = '..') then Continue;
if ((FileRec.Attr and faDirectory) <> 0) and
(FileRec.Name <> '.') and
(FileRec.Name <> '..') then
begin
Result := Result + GetFileCount(srcPath + FileRec.Name);
end else
if slFileResult.IndexOf(FileRec.Name) > -1 then
begin
pFR := PFileRecord(slFileResult.Objects[slFileResult.IndexOf(FileRec.Name)]);
pFR.iCount := pFR.iCount + 1;
end else
if slFileResult.IndexOf(FileRec.Name) = -1 then
begin
New(pFR);
pFR.sFileName := FileRec.Name;
pFR.iCount := 1;
slFileResult.AddObject(FileRec.Name, TObject(pFR));
end;
until FindNext(FileRec) <> 0;
end;
//该处代码用于求出指定路径下具有相同文件名的文件个数
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
pFR: PFileRecord;
iCount: Integer;
begin
if slFileResult = nil then
slFileResult := TStringList.Create;
Caption := IntToStr(GetFileCount(Edit1.Text));
for i := 0 to slFileResult.Count - 1 do
begin
pFR := PFileRecord(slFileResult.Objects[i]);
if pFR.iCount > 1 then
begin
iCount := iCount + 1;
Memo1.Lines.Add(Format('%d----%s', [pFR.iCount, pFR.sFilename]));
end;
end;
Caption := IntToStr(iCount);
end;本来想在一个函数内全部搞定的,但是涉及到递归函数调用,一个函数不能实现
看看是不是你要的结果,当然如果想求第一种输出的话,修改最后一个函数就可以了
分2步:
1、findfirst/findnext递归找出所有文件名到数据库
2、select filename.count(*) fc from t group by filename