function TForm1.GetALLFileOfCount(fDirName: string): Integer;
var
hFindFile:Cardinal;
TFile,ListName:string;
SCurDir:string[255];
FindFileData: WIN32_FIND_DATA;
begin
SCurDir:=GetCurrentDir;
ChDir(fDirname);
hFindFile:=FindFirstFile('*.*',FindFileData);
if fDirName[Length(fDirName)] <> '\' then
fDirName:=fDirName + '\';
if hFindFile <> INVALID_HANDLE_VALUE then
begin
repeat
begin
TFile:= FindFileData.cFileName;
if (TFile = '.') or (TFile = '..') then
Continue;
if FindFileData.dwFileAttributes=FILE_ATTRIBUTE_DIRECTORY then
begin
if fDirName[Length(fDirName)] <> '\' then
GetALLFileOfCount(fDirName+'\'+TFile)
else
GetALLFileOfCount(fDirName+TFile);
end
else
fCount:= fCount+1;
ListName := fDirName + TFile;
lst1.Items.Add(ListName + IntToStr(fCount));
end;
until FindNextFile(hFindFile,FindFileData) = False;
end
else
begin
ChDir(sCurDir);
CloseHandle(hFindFile);
exit;
end;
Result := fCount;
end;
procedure TForm1.btn1Click(Sender: TObject);
var
i : Integer;
begin
i:=GetALLFileOfCount(edt1.Text);
ShowMessage(IntToStr(i));
end;

看圖我發的截圖
為什么路徑也顯示出來了,而且后面的數字既然沒有+1,這個是怎么得來的,搞不懂,檢測到是目錄是沒有這個 lst1.Items.Add(ListName + IntToStr(fCount))代碼執行的,還是說我的代碼不夠嚴謹??望大神指點下,謝謝
uj5u.com熱心網友回復:
1、這是通過遞回方式,獲取指定目錄中的全部檔案名。2、你的箭頭指定的是目錄名,程式中只在非目錄時,fCount+1,所以它顯示的還是上一行的fCount值。
3、 ListName := fDirName + TFile就是路徑和檔案名兩個部分,加入到List中,自然顯示出來,含路徑目錄的檔案名了。
uj5u.com熱心網友回復:
function TForm1.GetALLFileOfCount(fDirName: string): Integer;
var
hFindFile:Cardinal;
TFile,ListName,Path:string;
SCurDir:string[255];
FindFileData: WIN32_FIND_DATA;
begin
SCurDir:=GetCurrentDir;
ChDir(fDirname);
hFindFile:=FindFirstFile('*.*',FindFileData);
if fDirName[Length(fDirName)] <> '\' then
fDirName:=fDirName + '\';
if hFindFile <> INVALID_HANDLE_VALUE then
begin
repeat
TFile:= FindFileData.cFileName;
if (TFile = '.') or (TFile = '..')
then Continue;
if FindFileData.dwFileAttributes=FILE_ATTRIBUTE_DIRECTORY then
begin
if fDirName[Length(fDirName)] <> '\'
then Path:=fDirName+'\'+TFile
else Path:=fDirName+TFile;
GetALLFileOfCount(Path)
end
else fCount:= fCount+1;
if FindFileData.dwFileAttributes<>FILE_ATTRIBUTE_DIRECTORY then
begin
ListName := fDirName + TFile;
ListBox1.Items.Add(ListName +' '+IntToStr(fCount));
end;
until FindNextFile(hFindFile,FindFileData) = False;
end
else begin
ChDir(sCurDir);
CloseHandle(hFindFile);
exit;
end;
Result := fCount;
end;
這樣,改一下。顯示全部檔案,排除目錄。并加序號
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/39568.html
標籤:語言基礎/算法/系統設計
上一篇:如何將Progress進度條結合這個SHFileOperation函式獲取進度值
下一篇:咱們論壇有開傳奇SF的嗎請教
