各位大神,帮小弟看看这代码是怎么回事,实在理解不过来

有戏倚生 2018-07-30 04:44:50

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))代码执行的,还是说我的代码不够严谨??望大神指点下,谢谢






...全文
224 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyhoo163 2018-07-30
  • 打赏
  • 举报
回复
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;

这样,改一下。显示全部文件,排除目录。并加序号
lyhoo163 2018-07-30
  • 打赏
  • 举报
回复
1、这是通过递归方式,获取指定目录中的全部文件名。
2、你的箭头指定的是目录名,程序中只在非目录时,fCount+1,所以它显示的还是上一行的fCount值。
3、 ListName := fDirName + TFile就是路径和文件名两个部分,加入到List中,自然显示出来,含路径目录的文件名了。

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧