erlang - How to delete the whole directory which is not empty? -
i want clean temporary collecting resource. file
module has del_dir/1 require directory empty. there not function files in directory (with absolute path")
the source code follows, how correct it?
delete_path(x)-> {ok,list} = file:list_dir_all(x), %% <--- return value has no absolute path here lager:debug("_229:~n\t~p",[list]), lists:map(fun(x)-> lager:debug("_231:~n\t~p",[x]), ok = file:delete(x) end,list), ok = file:del_dir(x), ok.
you can delete directory via console command using os:cmd, though it's rough approach. unix-like os be:
os:cmd("rm -rf " ++ dirpath).
if want delete non-empty directory using appropriate erlang functions have recursively. following example here shows how it:
-module(directory). -export([del_dir/1]). del_dir(dir) -> lists:foreach(fun(d) -> ok = file:del_dir(d) end, del_all_files([dir], [])). del_all_files([], emptydirs) -> emptydirs; del_all_files([dir | t], emptydirs) -> {ok, filesindir} = file:list_dir(dir), {files, dirs} = lists:foldl(fun(f, {fs, ds}) -> path = dir ++ "/" ++ f, case filelib:is_dir(path) of true -> {fs, [path | ds]}; false -> {[path | fs], ds} end end, {[],[]}, filesindir), lists:foreach(fun(f) -> ok = file:delete(f) end, files), del_all_files(t ++ dirs, [dir | emptydirs]).
Comments
Post a Comment