我按下一個按鈕,然后我就統計C盤里的具有相同檔案名的檔案的數量(只要輸出有多少個就行),(檔案名一樣就行,內容不用管)
uj5u.com熱心網友回復:
大神們 幫幫忙啊uj5u.com熱心網友回復:
急啊啊啊啊啊uj5u.com熱心網友回復:
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');
你試試,應該可以,這個包括子目錄,系統檔案夾等。
uj5u.com熱心網友回復:
c:> dir d:\dir\aaa.txt /s/auj5u.com熱心網友回復:
我只要c盤中具有相同檔案名的檔案個數,不用指定檔案來統計指定檔案的個數uj5u.com熱心網友回復:
c 盤中具有相同檔案名的檔案個數,要搜索多長時間。1、宣告動態陣列,保存搜索到的檔案名;
2、再提取動態陣列中的檔案名,搜索個數。
具體代碼,可以參考3樓。
uj5u.com熱心網友回復:
你的這個需求很怪,而且輸出內容描述不清楚,以括號內內容為例(指定路徑下共有x個A檔案,y個B檔案),我理解的意思有如下兩個:1.輸出x+y?
2.輸出2?
你要求的是哪種輸出?
uj5u.com熱心網友回復:
給你貼出上面第二種情況的代碼,自己研究吧,這種做法基本沒聽說有人這樣做過
//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;
本來想在一個函式內全部搞定的,但是涉及到遞回函式呼叫,一個函式不能實作
看看是不是你要的結果,當然如果想求第一種輸出的話,修改最后一個函式就可以了
uj5u.com熱心網友回復:
不是輸入檔案名,找它的同名檔案?分2步:
1、findfirst/findnext遞回找出所有檔案名到資料庫
2、select filename.count(*) fc from t group by filename
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/91700.html
標籤:語言基礎/算法/系統設計
上一篇:[當天結帖,跪求解決] -------------- [使用XMLHTTP方式獲取網頁原始碼,GBK亂碼]-----------------
下一篇:c++作業題sin公式
