我按下一个按钮,然后我就统计C盘里的具有相同文件名的文件的数量(只要输出有多少个就行),(文件名一样就行,内容不用管)
解决方案 »
- SQL语句
- delphi中怎么把一个整形数据转换成地址,赋值给一个数组作为首地址
- 如何取得FORM上的DRAWGRID的任一点的坐标?
- 有游戏开发需求
- 为什么我 use midaslib ,但是客户端还提示需要midas.dll 了呢?
- 怎样通过点击dbgrid中的记录弹出个对话框,然后删除,谢谢大家
- delphi真心指路
- 客户端群发信息给客户端的问题: 一条信息本来是先后发给2个客户端的,但由于缓冲作用,一条信息连着了同样的另一条信息一起发送了,怎办?
- 通过query查询后,query1.edit出错,
- 自学Delphi7,请前辈推荐书籍
- 改写clientdataset与中间层通讯数据报文格式,需要修改哪部分组件代码?
- BPL封装窗体问题
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