1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE erlref SYSTEM "erlref.dtd">
9
<holder>Ericsson AB, All Rights Reserved</holder>
12
The contents of this file are subject to the Erlang Public License,
13
Version 1.1, (the "License"); you may not use this file except in
14
compliance with the License. You should have received a copy of the
15
Erlang Public License along with this software. If not, it can be
16
retrieved online at http://www.erlang.org/.
18
Software distributed under the License is distributed on an "AS IS"
19
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
20
the License for the specific language governing rights and limitations
23
The Initial Developer of the Original Code is Ericsson AB.
26
<title>filelib</title>
27
<prepared>Bjorn Gustavsson</prepared>
28
<responsible>Bjorn Gustavsson</responsible>
30
<approved>Kenneth Lundin</approved>
34
<file>filelib.sgml</file>
36
<module>filelib</module>
37
<modulesummary>File utilities, such as wildcard matching of filenames</modulesummary>
39
<p>This module contains utilities on a higher level than the <c>file</c>
44
<title>DATA TYPES</title>
46
filename() = string() | atom() | DeepList
47
dirname() = filename()
48
DeepList = [char() | atom() | DeepList]</code>
53
<name>ensure_dir(Name) -> ok | {error, Reason}</name>
54
<fsummary>Ensure that all parent directories for a file or directory exist.</fsummary>
56
<v>Name = filename() | dirname()</v>
57
<v>Reason = posix() -- see file(3)</v>
60
<p>The <c>ensure_dir/1</c> function ensures that all parent
61
directories for the given file or directory name <c>Name</c>
62
exist, trying to create them if necessary.</p>
63
<p>Returns <c>ok</c> if all parent directories already exist
64
or could be created, or <c>{error, Reason}</c> if some parent
65
directory does not exist and could not be created for some
70
<name>file_size(Filename) -> integer()</name>
71
<fsummary>Return the size in bytes of the file.</fsummary>
73
<p>The <c>file_size</c> function returns the size of the given file.</p>
77
<name>fold_files(Dir, RegExp, Recursive, Fun, AccIn) -> AccOut </name>
78
<fsummary>Fold over all files matching a regular expression.</fsummary>
80
<v>Dir = dirname()</v>
81
<v>RegExp = regexp()</v>
82
<v>Recursive = true|false</v>
83
<v>Fun = fun(F, AccIn) -> AccOut</v>
84
<v>AccIn = AccOut = term()</v>
87
<p>The <c>fold_files/5</c> function folds the function
88
<c>Fun</c> over all (regular) files <c>F</c> in the
89
directory <c>Dir</c> that match the regular expression <c>RegExp</c>.
90
If <c>Recursive</c> is true all sub-directories to <c>Dir</c>
91
are processed. The match is tried on just the filename without
92
the directory part.</p>
96
<name>is_dir(Name) -> true | false</name>
97
<fsummary>Test whether Name refer to a directory or not</fsummary>
99
<v>Name = filename() | dirname()</v>
102
<p>The <c>is_dir/1</c> function returns <c>true</c> if <c>Name</c>
103
refers to a directory, and <c>false</c> otherwise.</p>
107
<name>is_file(Name) -> true | false</name>
108
<fsummary>Test whether Name refer to a file or directory.</fsummary>
110
<v>Name = filename() | dirname()</v>
113
<p>The <c>is_file/1</c> function returns <c>true</c> if <c>Name</c>
114
refers to a file or a directory, and <c>false</c> otherwise.</p>
118
<name>is_regular(Name) -> true | false</name>
119
<fsummary>Test whether Name refer to a (regular) file.</fsummary>
121
<v>Name = filename()</v>
124
<p>The <c>is_regular/1</c> function returns <c>true</c> if <c>Name</c>
125
refers to a file (regular file), and <c>false</c> otherwise.</p>
129
<name>last_modified(Name) -> {{Year,Month,Day},{Hour,Min,Sec}} | 0</name>
130
<fsummary>Return the local date and time when a file was last modified.</fsummary>
132
<v>Name = filename() | dirname()</v>
135
<p>The <c>last_modified/1</c> function returns the date and time the
136
given file or directory was last modified, or 0 if the file
141
<name>wildcard(Wildcard) -> list()</name>
142
<fsummary>Match filenames using Unix-style wildcards.</fsummary>
144
<v>Wildcard = filename() | dirname()</v>
147
<p>The <c>wildcard/1</c> function returns a list of all files
148
that match Unix-style wildcard-string <c>Wildcard</c>.</p>
149
<p>The wildcard string looks like an ordinary filename, except
150
that certain "wildcard characters" are interpreted in a special
151
way. The following characters are special:
156
<p>Matches one character.</p>
160
<p>Matches any number of characters up to the end of
161
the filename, the next dot, or the next slash.</p>
163
<tag>{Item,...}</tag>
165
<p>Alternation. Matches one of the alternatives.</p>
168
<p>Other characters represent themselves. Only filenames that
169
have exactly the same character in the same position will match.
170
(Matching is case-sensitive; i.e. "a" will not match "A").
172
<p>Note that multiple "*" characters are allowed
173
(as in Unix wildcards, but opposed to Windows/DOS wildcards).
176
<p>The following examples assume that the current directory is the
177
top of an Erlang/OTP installation.
179
<p>To find all <c>.beam</c> files in all applications, the following
180
line can be used:</p>
182
filelib:wildcard("lib/*/ebin/*.beam"). </code>
183
<p>To find either <c>.erl</c> or <c>.hrl</c> in all applications
184
<c>src</c> directories, the following</p>
186
filelib:wildcard("lib/*/src/*.?rl") </code>
187
<p>or the following line</p>
189
filelib:wildcard("lib/*/src/*.{erl,hrl}") </code>
191
<p>To find all <c>.hrl</c> files in either <c>src</c> or <c>include</c>
192
directories, use:</p>
194
filelib:wildcard("lib/*/{src,include}/*.hrl"). </code>
195
<p>To find all <c>.erl</c> or <c>.hrl</c> files in either
196
<c>src</c> or <c>include</c> directories, use:</p>
198
filelib:wildcard("lib/*/{src,include}/*.{erl,hrl}") </code>
202
<name>wildcard(Wildcard, Cwd) -> list()</name>
203
<fsummary>Match filenames using Unix-style wildcards starting at a specified directory.</fsummary>
205
<v>Wildcard = filename() | dirname()</v>
206
<v>Cwd = dirname()</v>
209
<p>The <c>wildcard/2</c> function works like <c>wildcard/1</c>,
210
except that instead of the actual working directory, <c>Cwd</c>