Skip to content

Instantly share code, notes, and snippets.

@robertoaloi
Forked from daleharvey/gist:304254
Created November 22, 2012 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertoaloi/4130202 to your computer and use it in GitHub Desktop.
Save robertoaloi/4130202 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, FileInfo} = file:read_file_info(NewFrom),
{ok, _} = file:copy(NewFrom, NewTo),
ok = file:write_file_info(NewTo, FileInfo);
false ->
ok
end
end.
@robertoaloi
Copy link
Author

This fork preserves file permissions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment