Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • Or search :

Idiom #180 List files in directory

Create list x containing the contents of directory d.

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

#include <dirent.h>
struct dirent ** x = NULL;
int n = scandir (p, &x, NULL, alphasort);
(def x ((comp file-seq clojure.java.io/file) d))
using System.IO
var files = Directory.GetFiles(dirPath);
import std.file;
auto x = dirEntries(d, SpanMode.shallow);
import 'dart:io';
var x = Directory(d).listSync();
import "io/ioutil"
x, err := ioutil.ReadDir(d)
import System.Directory (listDirectory)
do
  x <- listDirectory d
  -- do something with x
const fs = require('fs');
const x = fs.readdirSync(d)
import java.io.File;
final File directory = new File(d);
final File[] x = directory.listFiles();
$x = scandir($d);
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);
uses FileUtil;
x := FindAllFiles(d, '*', False);
opendir my $dh, $d or die "Could not open $d for reading: $!\n";
@x = readdir $dh;
closedir $dh;
use File::Basename;
my @x = map {basename $_} glob("$d/*");
import os
x = os.listdir(d)
x = Dir.children(d)
let x = std::fs::read_dir(d)?.collect::<Result<Vec<_>, _>()?;
use std::fs;
let x = fs::read_dir(d).unwrap();

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