Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Pascal

Idiom #180 List files in directory

Create the list x containing the contents of the directory d.

x may contain files and subfolders.
No recursive subfolder listing.

uses FileUtil;
x := FindAllFiles(d, '*', False);
uses SysUtils, Classes;
if FindFirst(IncludeTrailingPathDelimiter(d) + '*', 
               faAnyFile and (not faDirectory), SR) = 0 then
repeat
  x.Add(SR.Name);
until FindNext(SR) <> 0;
FindClose(SR);

A more classic implementation.
It will list only files, not the names of subdirectories inside p.
#include <dirent.h>
struct dirent ** x = NULL;
int n = scandir (p, &x, NULL, alphasort);

scandir allocates memory and returns the number of entries. each entry must be free'd. See also opendir, readdir and closedir and ftw for recursive traversal.

New implementation...
< >
programming-idioms.org