View Problem

Process each file in a directory recursively

ExpandDiskEdit
clojure
; (defn process-file [f] "process one file" body...)
(map process-file (file-seq (File. ".")))
ExpandDiskEdit
cpp C++/CLI .NET 2.0
void processFile(String^ filename) { Console::WriteLine("{0}", filename); }

void processDirectory(String^ dirname)
{
for each(String^ filename in IO::Directory::GetFiles(dirname)) processFile(filename);
for each(String^ subdirname in IO::Directory::GetDirectories(dirname)) processDirectory(subdirname);
}

int main()
{
processDirectory("c:\\");
}
ExpandDiskEdit
erlang
filelib:fold_files(Directory, ".*", true, fun (FileOrDirPath, Acc) -> Worker(FileOrDirPath), Acc end, []).
ExpandDiskEdit
erlang
process_dir(Directory, Worker).
ExpandDiskEdit
fantom
File(`./`).walk { process(it) }
ExpandDiskEdit
fsharp
let processDirectory dirname proc =
let rec processDirectory' dirname' =
Directory.GetFiles(dirname') |> Array.iter proc
Directory.GetDirectories(dirname') |> Array.iter processDirectory'
processDirectory' dirname

// ------

let dirname = "c:\\"

processDirectory dirname (fun filename -> printfn "%s" filename)
ExpandDiskEdit
groovy
dir.eachFileRecurse{ f -> process(f) }
ExpandDiskEdit
java
processDirectory(new File("c:\\"));
DiskEdit
ocaml
let rec recurse_dir dir f =
let filenames = Sys.readdir dir in
Array.iter (fun name ->
let fullname = Filename.concat dir name in
if Sys.is_directory fullname then
recurse_dir fullname f
else
f fullname
) filenames
;;
recurse_dir (Sys.getenv "HOME") print_endline ;;
DiskEdit
perl
use File::Glob;

process_directory(".");

sub process_directory {
my $dir = shift;
for my $file (<$dir/*>) {
next unless (-r $file);
if (-f $file) {
process_file($file);
} elsif (-d $file) {
process_directory($file);
}
}
}
DiskEdit
perl
use File::Find ();

# Traverse desired filesystems
sub process_directory {
my $directory = shift;
File::Find::find({wanted => \&wanted}, $directory);
}

sub wanted {
process_file( $File::Find::name );
}
ExpandDiskEdit
php

if ($dir_handle = opendir($dir))
list_dir($dir_handle);

function list_dir($dh) {
// as long as we can read the dir
while (($file = readdir($dh)) !== false) {
// if it's a dir and it's not the current dir nor the above dir
if (is_dir($file) && $file != '.' && $file !='..') {
// open the dir, print it's name and all files inside
$handle = opendir($file);
echo $file."\n";
list_dir($handle);
// if it's a simple file
} else if ($file != '.' && $file !='..') {
// type it's name
echo $file."\n";
}
}
closedir($dir_handle); // close the dirs
}
DiskEdit
python 2.4
import os
results = (process(os.path.join(p, n)) for p,d,l in os.walk(".") for n in l)
DiskEdit
ruby
def procdir(dirname)
Dir.foreach(dirname) do |dir|
dirpath = dirname + '/' + dir
if File.directory?(dirpath) then
if dir != '.' && dir != '..' then
puts "DIRECTORY: #{dirpath}" ; procdir(dirpath)
end
else
puts "FILE: #{dirpath}"
end
end
end

# ------

procdir('/tmp')
ExpandDiskEdit
scala
processDirectory(new File("c:\\"))

Submit a new solution for clojure, cpp, erlang, fantom ...
There is 1 other solution in haskell