List the files or subdirectories in a directory
From CodeCodex
Contents |
[edit] Implementations
[edit] 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
[edit] Haskell
import System.Directory(getDirectoryContents) main = do children <- getDirectoryContents "/path/to/dir" mapM_ putStrLn children
[edit] 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
[edit] Objective-C
NSString *dir = @"/path/to/dir";
NSArray *children = [[NSFileManager defaultManager] directoryContentsAtPath:dir];
for (NSString *filename in children)
NSLog(@"%@", filename);
[edit] OCaml
let dir = "/path/to/dir" in let children = Sys.readdir dir in Array.iter print_endline children;;
[edit] 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";
}
[edit] 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";
}
[edit] 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')
[edit] 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 }

