Difference between revisions of "List the files or subdirectories in a directory"
From CodeCodex
(→Python) |
(→Erlang) |
||
Line 10: | Line 10: | ||
</pre> | </pre> | ||
− | === | + | ===Erlang=== |
+ | <pre> | ||
+ | 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]), | ||
+ | ng | ||
+ | end. | ||
+ | </pre> | ||
+ | ===Haskell=== | ||
<pre> | <pre> | ||
import System.Directory (getDirectoryContents) | import System.Directory (getDirectoryContents) | ||
Line 139: | Line 150: | ||
[[Category:Common Lisp]] | [[Category:Common Lisp]] | ||
+ | [[Category:Erlang]] | ||
[[Category:Haskell]] | [[Category:Haskell]] | ||
[[Category:Java]] | [[Category:Java]] |
Revision as of 14:24, 17 January 2011
Contents
Implementations
Common Lisp
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
Erlang
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]), ng end.
Haskell
import System.Directory (getDirectoryContents) lisFiles = getDirectoryContents "/path/to/dir" >>= filterM (fmap not . doesDirectoryExist) >>= mapM_ putStrLn . reverse listAll = getDirectoryContents "/path/to/dir" >>= mapM_ putStrLn . reverse
Java
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);
- Original Source: The Java Developers Almanac 1.4
Objective-C
NSString *dir = @"/path/to/dir"; NSArray *children = [[NSFileManager defaultManager] directoryContentsAtPath:dir]; for (NSString *filename in children) NSLog(@"%@", filename);
OCaml
let dir = "/path/to/dir" in let children = Sys.readdir dir in Array.iter print_endline children;;
Perl
# 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"; }
PHP
// if you know the exact name: $handle = opendir('/path/to/dir'); while (false !== ($file = readdir($handle))) { echo "$file\n"; } closedir($handle); // if you want shell-style globbing: foreach (glob('/path/to/dir/*.html') as $file){ echo "$file\n"; }
Python
# 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')
Ruby
# if you know the exact name: d = Dir.new('/path/to/dir') d.each {|file| puts file } d.close # or Dir.open('/path/to/dir') do |d| d.each {|file| puts file } end # or Dir.foreach('/path/to/dir') {|file| puts file } # if you want shell-style globbing: Dir['/path/to/dir/*.html'].each { |file| puts file }