List the files or subdirectories in a directory

From CodeCodex

Revision as of 12:17, 6 July 2012 by (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Common Lisp[edit]

Note that the code below is extremely brittle and implementation-dependent; for historical reasons CL handling of directories and paths is very complex and hard to use without wrapper libraries. Use CL-FAD or similar.

(directory "*") ;lists all files and folders in current directory
(directory "/Users/Sathya/*") ;lists all files in specified folder
(directory "*.txt") ;lists all .txt files in current directory


list_dir(Dir) ->
    case file:list_dir(Dir) of
        {ok, Filenames} ->
            lists:foreach(fun(Name) -> io:format("~s~n", [Name]) end, Filenames);
        {error, enoent} ->
            io:format("The directory(~s) does not exist.~n", [Dir]),


import System.Directory (getDirectoryContents)

lisFiles = getDirectoryContents "/path/to/dir" >>=  
           filterM (fmap not . doesDirectoryExist) >>=
           mapM_ putStrLn . reverse

listAll  = getDirectoryContents "/path/to/dir" >>=
           mapM_ putStrLn . reverse


This example lists the files and subdirectories in a directory. To list all descendant files and subdirectories under a directory, see Traversing the Files and Directories Under a Directory.

    File dir = new File("directoryName");
    String[] children = dir.list();
    if (children == null) {
        // Either dir does not exist or is not a directory
    } else {
        for (int i=0; i<children.length; i++) {
            // Get filename of file or directory
            String filename = children[i];
    // It is also possible to filter the list of returned files.
    // This example does not return any files that start with `.'.
    FilenameFilter filter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return !name.startsWith(".");
    children = dir.list(filter);
    // The list of files can also be retrieved as File objects
    File[] files = dir.listFiles();
    // This filter only returns directories
    FileFilter fileFilter = new FileFilter() {
        public boolean accept(File file) {
            return file.isDirectory();
    files = dir.listFiles(fileFilter);


NSString *dir = @"/path/to/dir";

NSArray *children = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dst error:nil];
for (NSString *filename in children)
    NSLog(@"%@", filename);


let dir = "/path/to/dir" in
let children = Sys.readdir dir in
Array.iter print_endline children;;


# if you know the exact name:
opendir my $handle, '/path/to/dir';
foreach (readdir $handle) {
    print "$_\n";
closedir $handle;

# if you want shell-style globbing:
while (</path/to/dir/*.html>) {
    print "$_\n";


// if you know the exact name:
$handle = opendir('/path/to/dir');
while (false !== ($file = readdir($handle))) {
    echo "$file\n";

// if you want shell-style globbing:
foreach (glob('/path/to/dir/*.html') as $file){
    echo "$file\n";


# if you know the exact name:
import os
files = os.listdir('/path/to/dir/')

# if you want shell-style globbing:
import glob
files = glob.glob('/path/to/dir/*.html')


# if you know the exact name:
d ='/path/to/dir')
d.each {|file| puts file }

# or'/path/to/dir') do |d|
  d.each {|file| puts file }

# or
Dir.foreach('/path/to/dir') {|file| puts file }

# if you want shell-style globbing:
Dir['/path/to/dir/*.html'].each { |file| puts file }