-module(find). %%#doc maintainers(["joe@cslab.ericsson.se (Joe Armstrong)"]). %%#doc category("Unix like utilities"). %%#doc title("Find (recursivly) files matching RegExp"). %%#doc keywords(["find"]). -export([files/3, out_of_date/3]). -import(lists, [suffix/2, sublist/3, map/2, filter/2]). %%#spec type files(dir::string(), regexp::string(), recursive::bool()) -> %%# [file()]. %%#doc summary("find:files(Dir, RegExp, Recursive) find regular files %%# starting from Dir which match ReExpr. %%# If Recursive is true do recursivly on all sub-directories. %%# Example find(\".\", \"*.erl\", false) will find all erlang %%# files in the cCurrent directory. %%# %%#
out_of_date(Dir, SrcExt, ObjExt) find %%# all \"out of date files\" in Dir. Example: %%# out_of_date(\".\", \".erl\", \".jam\") %%# Finds all out of date files in the current directory"). files(Dir, Re, Flag) -> Re1 = string:re_sh_to_awk(Re), find_files(Dir, Re1, Flag, []). %% +type find_files(dirname(), Regexp, bool(), [filename()]) -> [filename()] %% when Regexp = string(). find_files(Dir, Re, Flag, L) -> case file:list_dir(Dir) of {ok, Files} -> find_files(Files, Dir, Re, Flag, L); {error, _} -> L end. %% +type find_files([filename()], dirname(), Regexp, bool(), [filename()]) -> %% [filename()] when Regexp = string(). find_files([File|T], Dir, Re, Recursive, L) -> FullName = Dir ++ [$/|File], case file_type(FullName) of regular -> case string:re_match(FullName, Re) of {match, _, _} -> find_files(T, Dir, Re, Recursive, [FullName|L]); _ -> find_files(T, Dir, Re, Recursive, L) end; directory -> case Recursive of true -> L1 = find_files(FullName, Re, Recursive, L), find_files(T, Dir, Re, Recursive, L1); false -> find_files(T, Dir, Re, Recursive, L) end; error -> find_files(T, Dir, Re, Recursive, L) end; find_files([], _, _, _, L) -> L. %% +type file_type(string()) -> regular | directory | error. -include_lib("kernel/include/file.hrl"). file_type(File) -> case file:read_file_info(File) of {ok, Facts} -> case Facts#file_info.type of regular -> regular; directory -> directory; _ -> error end; _ -> error end. %%______________________________________________________________________ %% outofdate(Dir, InExtension, OutExtension) %% scans Dir for all files with the extension "InExtension" %% If a file with this extension is found then "OutExtension" is checked %% %% returns a list of files in