delphi 取文件目录下所有文件名和文件目录名


本文整理自网络,侵删。

 
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


uses
  // Delphi
 ShlObj, ActiveX, Character, Math, DateUtils;


function IsDirectory(const DirName: string): Boolean;
begin
  Result := DirectoryExists(DirName);
end;


function ListFiles(const Dir, Wildcard: string; const List: TStrings; IncludeDirs: Boolean = True; RelativeNames: Boolean = False): Boolean;
var
  FileSpec: string;   // full file spec of a wildcard
  Path: string;       // full path of directory, including training backslash
  SR: TSearchRec;     // file search result
  Success: Integer;   // success code for FindXXX routines
const
  faVolumeId = $00000008; // redefined from SysUtils to avoid deprecated warning
begin
  Assert(Assigned(List), 'ListFiles: List is nil');
  // Check if true directory and exit if not
  Result := IsDirectory(Dir);
  if not Result then
    Exit;
  // Build FileSpec from directory and wildcard
  FileSpec := IncludeTrailingPathDelimiter(Dir);
  if Wildcard = '' then
    FileSpec := FileSpec + '*.*'
  else
    FileSpec := FileSpec + Wildcard;
  Path := IncludeTrailingPathDelimiter(Dir);
  // Do search
  Success := FindFirst(FileSpec, faAnyFile, SR);
  try
    while Success = 0 do
    begin
      // only add true files or directories to list
      if (SR.Name <> '.') and (SR.Name <> '..')
        and (SR.Attr and faVolumeId = 0)
        and (IncludeDirs or not IsDirectory(Path + SR.Name)) then
        if RelativeNames then
          List.Add(SR.Name)
        else
          List.Add(Path + SR.Name);
      Success := FindNext(SR);
    end;
  finally
    System.SysUtils.FindClose(SR);
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
ListFiles('C:\Users\xxx\Desktop\codesnip-develop\codesnip-develop','*.*',memo1.Lines,true{是否获取文件目录},true{是否只显示文件名});
end;

end.

相关阅读 >>

Delphi 编写的一个感染文件夹的小病毒

Delphi cef4Delphi chromium1 设置user-agent

Delphi编写dll(以及静态和动态方式调用)

Delphi比较两个位图是否相同

Delphi 用空格把 s 凑够 n 的长度

Delphi sendmessage这个函数有很多奇妙的用途

Delphi android windows ios通用获取程序版本

Delphi idhttp中设置非标准头信息和读写cookie

Delphi获取系统电源状态的信息

高手谈做程序员的8大基本原则

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...