Erlang/OTP Forums

Author Message

<  Erlang  ~  Need some help on first Erlang appliation.

JarrodRoberson
Posted: Mon Sep 17, 2007 9:41 pm Reply with quote
User Joined: 17 Sep 2007 Posts: 42
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 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?
View user's profile Send private message
Mazen
Posted: Tue Sep 18, 2007 9:17 am Reply with quote
User Joined: 20 Jul 2006 Posts: 164 Location: London
On top of my head it shouldn't be to hard...

Start by writing it in psuedo code (as a functional program) and then have a look at these pages for the functions you need:

http://www.erlang.org/doc/man/filelib.html
http://www.erlang.org/doc/man/filename.html
View user's profile Send private message
JarrodRoberson
Posted: Wed Sep 19, 2007 3:34 pm Reply with quote
User Joined: 17 Sep 2007 Posts: 42
I am struggling with the "as a functional program" part. :-)

I see that there is filelib:fold_files() and filelib:file_size() but I can't figure out what to feed fold_files() the documentation is pretty sparse on what the parameters are. I have searched and searched and don't understand what is expected for "AccIn" the last parameter to fold_files().

I have a sum() function working from an example I found online that works on a list. I just need to understand how to get a list of file sizes from filelib:foldfiles()

Can anyone explain?
View user's profile Send private message
michal
Posted: Wed Sep 19, 2007 4:47 pm Reply with quote
User Joined: 20 Jul 2006 Posts: 44 Location: London
AccIn stands for accumulator. You want to calculate the sum of all file sizes so initiate it with 0. The fun you are passing as the fourth parameter should take two parameters - one is the file name, the other is the current value of the accumulator. The fun should return the new value of the accumulator.
Now when all is clear try this code:

filelib:fold_files(".", ".+", true, fun(F, A) -> A+filelib:file_size(F) end, 0).

Michal

_________________
http://www.erlang-consulting.com
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum