Skip to content

Instantly share code, notes, and snippets.

@daleharvey
Created February 14, 2010 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save daleharvey/304254 to your computer and use it in GitHub Desktop.
Save daleharvey/304254 to your computer and use it in GitHub Desktop.
%% Recursively copy directories
-spec recursive_copy(list(), list()) -> ok.
recursive_copy(From, To) ->
{ok, Files} = file:list_dir(From),
[ok = rec_copy(From, To, X) || X <- Files],
ok.
-spec rec_copy(list(), list(), list()) -> ok.
rec_copy(_From, _To, [$. | _T]) -> %% Ignore Hidden
ok;
rec_copy(From, To, File) ->
NewFrom = filename:join(From, File),
NewTo = filename:join(To, File),
case filelib:is_dir(NewFrom) of
true ->
ok = filelib:ensure_dir(NewTo),
recursive_copy(NewFrom, NewTo);
false ->
case filelib:is_file(NewFrom) of
true ->
ok = filelib:ensure_dir(NewTo),
{ok, _} = file:copy(NewFrom, NewTo),
ok;
false ->
ok
end
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment