Delphi 版 everything、光速搜索代码》,文章中关于获取文件全路径的函数:GetFullFileName,有一个地方值得优化。

就是有多个文件,它们可能属于同一个目录。

譬如 System32 目录下有2000多个文件,GetFullFileName 还是进行了2000多次的查询,效率肯定是受影响的。

先处理目录,获取目录全路径名称。

然后文件只用查询一次,就知道它的父路径的全路径了。效率肯定会提高的。尝试了一下。

  1. { 获取文件全路径,包含路径和文件名 }
  2. procedure GetFullFileName(var FileList: TStringList; const chrLogiclDiskName: Char; const bSort: Boolean = False);
  3. var
  4. UInt64DirList    : TArray<UInt64>;
  5. III              : Integer;
  6. UPID             : UInt64;
  7. intIndex         : Integer;
  8. dirList          : TStringList;
  9. intDirectoryCount: Integer;
  10. begin
  11. { 将 FileList 按 FileReferenceNumber 数值排序 }
  12. FileList.Sorted := False;
  13. FileList.CustomSort(Int64Sort);
  14. { 先处理目录,获取路径的全路径名称 }
  15. dirList := TStringList.Create;
  16. try
  17. { 获取目录的总数 }
  18. intDirectoryCount := 0;
  19. for III           := 0 to FileList.Count - 1 do
  20. begin
  21. if PFileInfo(FileList.Objects[III])^.bDirectory then
  22. begin
  23. Inc(intDirectoryCount);
  24. end;
  25. end;
  26. SetLength(UInt64DirList, intDirectoryCount);
  27. { 将所有目录信息添加到目录列表 }
  28. intDirectoryCount := 0;
  29. for III           := 0 to FileList.Count - 1 do
  30. begin
  31. if PFileInfo(FileList.Objects[III])^.bDirectory then
  32. begin
  33. dirList.AddObject(PFileInfo(FileList.Objects[III])^.strFileName, FileList.Objects[III]);
  34. UInt64DirList[intDirectoryCount] := PFileInfo(FileList.Objects[III])^.FileReferenceNumber;
  35. Inc(intDirectoryCount);
  36. end;
  37. end;
  38. { 获取目录的全路径名称 }
  39. intDirectoryCount := 0;
  40. for III           := 0 to FileList.Count - 1 do
  41. begin
  42. if PFileInfo(FileList.Objects[III])^.bDirectory then
  43. begin
  44. UPID := PFileInfo(FileList.Objects[III])^.ParentFileReferenceNumber;
  45. while TArray.BinarySearch(UInt64DirList, UPID, intIndex) do
  46. begin
  47. UPID                  := PFileInfo(dirList.Objects[intIndex])^.ParentFileReferenceNumber;
  48. FileList.Strings[III] := PFileInfo(dirList.Objects[intIndex])^.strFileName + '\' + FileList.Strings[III];
  49. end;
  50. FileList.Strings[III]              := (chrLogiclDiskName + ':\' + FileList.Strings[III]);
  51. dirList.Strings[intDirectoryCount] := FileList.Strings[III];
  52. Inc(intDirectoryCount);
  53. end;
  54. end;
  55. { 再获取每个文件的全路径 }
  56. for III := 0 to FileList.Count - 1 do
  57. begin
  58. if not PFileInfo(FileList.Objects[III])^.bDirectory then
  59. begin
  60. UPID := PFileInfo(FileList.Objects[III])^.ParentFileReferenceNumber;
  61. if TArray.BinarySearch(UInt64DirList, UPID, intIndex) then
  62. begin
  63. FileList.Strings[III] := dirList.Strings[intIndex] + '\' + FileList.Strings[III];
  64. end
  65. else
  66. begin
  67. FileList.Strings[III] := chrLogiclDiskName + '\' + FileList.Strings[III];
  68. end;
  69. end;
  70. end;
  71. { 将所有文件按文件名排序 }
  72. if bSort then
  73. FileList.Sort;
  74. finally
  75. dirList.Free;
  76. end;
  77. end;

这个函数比原来的函数效率上刚好提高了一倍。

100万个的文件,耗时4秒左右。200万个的文件,耗时8秒左右。

注:原有的  TFileInfo 添加个目录属性:

TFileInfo = record
    strFileName: String;               // 文件名称
    bDirectory: Boolean;               // 是否是目录 <增加>
    FileReferenceNumber: UInt64;       // 文件的ID
    ParentFileReferenceNumber: UInt64; // 文件的父ID

end;

在代码

FileList.AddObject(strFileName, TObject(pfi));

前,添加一行:

pfi^.bDirectory  := UsnRecord^.FileAttributes and FILE_ATTRIBUTE_DIRECTORY = FILE_ATTRIBUTE_DIRECTORY;

[转载]《Delphi 版 everything、光速搜索代码》 关于获取文件全路径 GetFullFileName 函数的优化的更多相关文章

  1. [转载]Delphi 版 everything、光速搜索代码

    近日没啥事情,研究了一下 everything.光速搜索原理.花了一个礼拜时间,终于搞定. 废话不多说,直接上代码: unit uMFTSearchFile; { dbyoung@sina.com 2 ...

  2. Delphi获取文件名、文件名不带扩展名、文件名的方法;delphi 获取文件所在路径

    取文件名 ExtractFileName(FileName); 取文件扩展名: ExtractFileExt(filename); 取文件名,不带扩展名: 方法一:   Function Extrac ...

  3. java版ftp简易客户端(可以获取文件的名称及文件大小)

    java版ftp简易客户端(可以获取文件的名称及文件大小) package com.ccb.ftp; import java.io.IOException; import java.net.Socke ...

  4. 教程-Delphi中的GExperts搜索代码快捷键

    Shift+Ait+S  打开搜索 Ctrl+Ait+R 打开上次搜索结果

  5. delphi根据进程PID获取程序所在路径的函数(用OpenProcess取得句柄,用GetModuleFileNameEx取得程序名)

    uses psapi; {根据进程PID获取程序所在路径的函数}function GetProcessExePath(PID: Cardinal): string;varpHandle: THandl ...

  6. 拖放获取文件信息的bat代码

    参考:岁月如歌-通过拖曳获取文件信息的bat代码 拖放获取文件信息的bat代码 使用命令行配合7z解压文件时由于每次解压的文件不同,因此搜索了一下拖放识别文件信息的方法,以此方式来减轻工作量 获取文件 ...

  7. Delphi获取文件名、不带扩展名文件名、文件所在路径、上级文件夹路径的方法

    1.获取不带扩展名的文件名方法,利用ChangeFileExt函数修改传入参数的扩展为空,并不会对文件本身产生变更. ChangeFileExt(ExtractFileName('D:\KK\Test ...

  8. 通过崩溃地址找错误行数之Delphi版

    通过崩溃地址找错误行数之Delphi版2009-5-11 17:42:35 来源: 转载 作者:网络 访问:360 次 被顶:2 次 字号:[大 中 小]核心提示:什么是 MAP 文件?简单地讲, M ...

  9. 雪花算法(snowflake)delphi版

    雪花算法简单描述: + 最高位是符号位,始终为0,不可用. + 41位的时间序列,精确到毫秒级,41位的长度可以使用69年.时间位还有一个很重要的作用是可以根据时间进行排序. + 10位的机器标识,1 ...

随机推荐

  1. MiCode108 猜数字

    Description 相传,十八世纪的数学家喜欢玩一种猜数字的小游戏,规则如下: 首先裁判选定一个正整数数字 N (2 \leq N \leq 200)N(2≤N≤200),然后选择两个不同的整数X ...

  2. django自带的django.core.mail模块实现发邮件的功能

    django自带了一个模块,可以实现发邮件的功能.如果项目开发遇到需要发邮件进行验证的时候可以用到. 1.先要准备发件人 发邮件需要使用SMTP.SMTP是什么呢? 简单邮件传输协议(Simple M ...

  3. 阻止父类的create,是无法阻止的

  4. 聊聊五大IO模型

    IO模型介绍 IO模型不是用来开启并发效果的,而是用来接收并发效果的. 比较了五种IO Model:    * blocking IO           阻塞IO    * nonblocking ...

  5. ubuntu 软件包(package)更换源(source)为阿里云镜像 update&upgrade

    在ubuntu下用apt-get install安装软件时,发现package list中没有所需的软件, 估计可能是package list太旧了,于是需要apt-get update & ...

  6. 【转】kubernetes 中 deployment 支持哪些键值

    这个比较全,可以参考 ================= https://www.addops.cn/post/kubernetes-deployment-fileds.html ========== ...

  7. PAT L3-002. 堆栈

    树状数组,二分. 一堆数字,可以删除栈顶,压入数字,求中位数,可以线段树,也可以树状数组上二分. #include<map> #include<set> #include< ...

  8. LoggerAspect

    package nc.oss.utils; import java.util.Date; import nc.bs.framework.common.InvocationInfoProxy; impo ...

  9. JavaScript函数的防抖和节流

    防抖 触发高频事件后n秒内函数只会执行一次,如果n秒内高频事件再次被触发,则重新计算时间 思路: 每次触发事件时都取消之前的延时调用方法 function debounce(fn) { let tim ...

  10. Flask实战第44天:完成前台注册功能

    注册功能后端逻辑 用户注册要把注册的表单提交上来,因此,我要先对表单进行验证,编辑front.forms from apps.forms import BaseForm from wtforms im ...