我最近寫了一個文件系統索引和在這裏下面,我爲您提供一個模塊,它可以遍歷一個或多個目錄,並讓您知道您想要的文件或找到的文件夾。它所做的是,它將spawn
一個新的進程來處理任何內部目錄。您將提供兩個Functional Objects
,一個用於處理目錄,另一個用於處理文件。
%% @doc This module provides the low level APIs for reading, writing,
%% searching, joining and moving within directories.
%% @end
-module(file_scavenger_utilities).
%%% ------- EXPORTS -------------------------------------------------------------------------------
-compile(export_all).
%%% ------- INCLUDES -----------------------------------------------------------------------------
%%% -------- MACROS ------------------------------------------------------------------------------
-define(IS_FOLDER(X),filelib:is_dir(X)).
-define(IS_FILE(X),filelib:is_file(X)).
-define(FAILED_TO_LIST_DIR(X),
error_logger:error_report(["* File Scavenger Utilities Error * ",
{error,"Failed to List Directory"},{directory,X}])).
-define(NOT_DIR(X),
error_logger:error_report(["* File Scavenger Utilities Error * ",
{error,"Not a Directory"},{alleged,X}])).
-define(NOT_FILE(X),
error_logger:error_report(["* File Scavenger Utilities Error * ",
{error,"Not a File"},{alleged,X}])).
%%%--------- TYPES -------------------------------------------------------------------------------
%% @type dir() = string().
%% Must be containing forward slashes, not back slashes. Must not end with a slash
%% after the exact directory.e.g this is wrong: "C:/Program Files/SomeDirectory/"
%% but this is right: "C:/Program Files/SomeDirectory"
%% @type file_path() = string().
%% Must be containing forward slashes, not back slashes.
%% Should include the file extension as well e.g "C:/Program Files/SomeFile.pdf"
%% -----------------------------------------------------------------------------------------------
%% @doc Enters a directory and executes the fun ForEachFileFound/2 for each file it finds
%% If it finds a directory, it executes the fun %% ForEachDirFound/2.
%% Both funs above take the parent Dir as the first Argument. Then, it will spawn an
%% erlang process that will spread the found Directory too in the same way as the parent directory
%% was spread. The process of spreading goes on and on until every File (wether its in a nested
%% Directory) is registered by its full path.
%% @end
%%
%% @spec new_user(dir(),funtion(),function())-> ok.
spread_directory(Dir,Top_Directory,ForEachFileFound,ForEachDirFound)
when is_function(ForEachFileFound),is_function(ForEachDirFound) ->
case ?IS_FOLDER(Dir) of
false -> ?NOT_DIR(Dir);
true ->
F = fun(X)->
FileOrDir = filename:absname_join(Dir,X),
case ?IS_FOLDER(FileOrDir) of
true ->
(catch ForEachDirFound(Top_Directory,FileOrDir)),
spawn(fun() ->
?MODULE:spread_directory(FileOrDir,Top_Directory,ForEachFileFound,ForEachDirFound)
end);
false ->
case ?IS_FILE(FileOrDir) of
false -> {error,not_a_file,FileOrDir};
true -> (catch ForEachFileFound(Top_Directory,FileOrDir))
end
end
end,
case file:list_dir(Dir) of
{error,_} -> ?FAILED_TO_LIST_DIR(Dir);
{ok,List} -> lists:foreach(F,List)
end
end.
要測試,下面是使用:
E:\Applications>erl
Eshell V5.9 (abort with ^G)
1> Dir = "E:/Ruth".
"E:/Ruth"
2> TopDir = "E:/Ruth".
"E:/Ruth"
3> Folder = fun(Parent,F) -> io:format("\n\t~p contains Folder: ~p~n",[Parent,F]) end.
#Fun<erl_eval.12.111823515>
4> File = fun(Parent,F) -> io:format("\n\t~p contains File: ~p~n",[Parent,F]) end.
#Fun<erl_eval.12.111823515>
5> file_scavenger_utilities:spread_directory(Dir,TopDir,File,Folder).
"E:/Ruth" contains File: "e:/Ruth/Thumbs.db"
"E:/Ruth" contains File: "e:/Ruth/Robert Passport.pdf"
"E:/Ruth" contains File: "e:/Ruth/new mixtape.mp3"
"E:/Ruth" contains File: "e:/Ruth/Manning - Java 3d Programming.pdf"
"E:/Ruth" contains File: "e:/Ruth/jcrea350.zip"
"E:/Ruth" contains Folder: "e:/Ruth/java-e books"
"E:/Ruth" contains File: "e:/Ruth/Java Programming Unleashed.pdf"
"E:/Ruth" contains File: "e:/Ruth/Java Programming Language Basics.pdf"
"E:/Ruth" contains File: "e:/Ruth/java-e books/vb blackbook.pdf.pdf"
[FileLib文件:fold_files](http://www.erlang.org/doc/man/filelib.html#fold_files-5)可能是你正在尋找的功能。 – legoscia