e r l a n g : c o o k b o o k

/ Erlang.FileWalkDirectoryTree

This Web


WebHome 
WebChanges 
TOC
NewRecipe 
WebTopicList 
WebStatistics 

All Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Erlang Links


Erlang.org
Erlang Wiki
ErlMan
Erlang Wiki
The Jungerl
Erlang-fr.org
Joe Armstrong
Lambda the Ultimate

Erlang Web Ring


[Prev]: Joe Armstrong's Page
[Next]: Joe Armstrong's Page

Walking Directory Trees

Problem

You need to look at every file in a directory, or an entire directory tree, that matches a given pattern.

Solution

wildcard function in the Erlang standard library filelib module is a very flexible way to do this. wildcard takes as input a string containing standard UNIX wild card elements (such as the *, ?, or { list-of-alternatives }) and returns a list of file names that satisfy the criteria:
% list all files and directories under /tmp
1> filelib:wildcard("/tmp/*").
["/tmp/.ICE-unix",
 "/tmp/.X11-unix",
 "/tmp/.font-unix",
 "/tmp/test1.erl",
 "/tmp/test2.erl",
 "/tmp/testB.erl",
 "/tmp/testC.erl",
 "/tmp/wings.erl"]
You can provide finer-grained control over the results by using some of the list primitives, such as filter or fold, along with various file operations and predicates to only provide results of interest to you:
% list all subdirectories of /tmp
2> lists:filter(fun(X) -> filelib:is_dir(X) end, filelib:wildcard("/tmp/*")).
["/tmp/.ICE-unix",
 "/tmp/.X11-unix",
 "/tmp/.font-unix",
 "/tmp/testC.erl"]

% list all files with extension .erl under /tmp
3> lists:filter(fun(X) -> not filelib:is_dir(X) end,
3>     filelib:wildcard("/tmp/*.erl")).
["/tmp/test1.erl","/tmp/test2.erl","/tmp/testB.erl","/tmp/wings.erl"]

% list all files under /tmp that are > 1024 bytes
4> lists:filter(fun(X) -> not filelib:is_dir(X)
4>    andalso filelib:file_size(X) > 1024 end, 
4>    filelib:wildcard("/tmp/*.erl")).
["/tmp/wings.erl"]

Discussion

The examples given all just act on metadata of the file, there's no reason you can't inspect the contents of the file in the filter function, though that will make the function much slower.

Based on work by GordonWeakliem.

-- BrentAFulgham - 02 Sep 2004

CookbookForm
TopicType: Recipe
ParentTopic: FileRecipes
TopicOrder: 060

 
 
Copyright © 2004 by the contributing authors. All material on the Erlang Cookbook web site is the property of the contributing authors.
This material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest