Jump to content

[Delphi] List file names under a folder (with filtering)

- - - - -

  • Please log in to reply
No replies to this topic

#1
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 763 posts
The following proceduce will fill AList with names of files under folder specified by AFolder match with mask/filter specified by AFilter.

Example:
  • To get all files under "C:\Tmp" folder to a memo:
    ListFiles('C:\Tmp\', '*.*', Memo1.Lines);
  • To list files with "txt" extension under "C:\Tmp" folder to a memo:
    ListFiles('C:\Tmp\', '*.txt', Memo1.Lines);
  • To list files with names start with "My" under "C:\Tmp" folder to a memo:
    ListFiles('C:\Tmp\', 'My*.*', Memo1.Lines);


procedure ListFiles(const AFolder, AFilter: string; AList: TStrings);

var

  vFindHandle: THandle;

  vFilter    : String;

  vFindData  : WIN32_FIND_DATA;

begin

  AList.BeginUpdate;

  try

    AList.Clear;

    vFilter := AFolder + '\' + AFilter;

    vFindHandle := FindFirstFile(PChar(vFilter), vFindData);

    if vFindHandle = INVALID_HANDLE_VALUE then

      Exit;


    repeat

      AList.Add(ChangeFileExt(vFindData.cFileName, ''));

    until not FindNextFile(vFindHandle, vFindData);

    Windows.FindClose(vFindHandle);

  finally

    AList.EndUpdate;

  end;

end;






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users