用Erlang实现递归查找文件

实现代码如下

 1 -module(recursionsearch).
 2 -export([start/1]).
 3 
 4 start(Dir) ->
 5     L = search_cur_dir(Dir, []),
 6     io:format("~p~n", [L]).
 7 
 8 %  在当前文件夹中搜索指定文件
 9 search_cur_dir(Dir, L) ->
10     FunAddPath = fun(File) -> filename:join(Dir, File) end,
11     FindList = lists:map(FunAddPath, filelib:wildcard("*.erl", Dir)),
12     case file:list_dir(Dir) of
13         {ok, Filenames} -> 
14             recursion(Dir, Filenames, FindList ++ L);
15         {error, Reason} -> {error, Reason}
16     end.
17 
18 % 在当前文件夹的子目录中进行搜索
19 recursion(Dir, [H|T], L) ->
20     DirFlag=filelib:is_dir(filename:join(Dir, H)),
21     case DirFlag of 
22         true -> recursion(Dir, T, search_cur_dir(filename:join(Dir, H), L)); 
23         false -> recursion(Dir, T, L)
24     end;
25 
26 recursion(_, [], L) ->
27     L.

 

posted on 2013-01-06 15:58  应无所住而生其心  阅读(373)  评论(0编辑  收藏  举报

导航