[erlang-questions] du in Erlang as a first attempt at an application

Minsloc Tarren minsloc@REDACTED
Tue Sep 18 11:01:13 CEST 2007

Something like this should work

dirsize(Dir) ->
  {Dirs,Files} = listdir(Dir),
  sum([filesize(File) || File <- Files])      % iterate over Files,
collecting return values of filesize(<file>), then sum the list
    + sum([dirsize(Dir2) || Dir2 <- Dirs]).  % ditto for for directories

Not so effective, but quite elegant, I hope ;-)

Does not detect circular links ...

On 9/18/07, Jarrod Roberson <jarrod@REDACTED> wrote:
> I am trying to grok how to write a simple 'du' like program that walks
> a directory structure ( not just one but all nested directories ) and
> calculates a sum of all the file sizes. I found some sum() code that I
> understand how that works.
> I have stared at my Programming Erlang book I got a few days ago, and
> stared at the Erlang Cookbook as well as staring and hacking at the
> erlang shell and it just isn't clicking.
> >From what I see, this should be like a 5 line program in Erlang.
> Can anyone provide a working example or point me in the correct direction?
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20070918/8f18616a/attachment.htm>

More information about the erlang-questions mailing list