4
votes

I need in function in erlang which will be count files in something directory. I write this function:

files_count(dir) ->
    case file:list_dir(dir) of  
         {ok, FileNames} ->
            length(FileNames);
        {error, Reason} ->
            Reason
    end.

When i try to test it. I run in erlang shell for example:

1> module:files_count(/home/).

I see exceptrion: ** exception error: no function clause matching module:files_count("/home/")

What's wrong?

Thank you.

1

1 Answers

4
votes
-module(countfiles).
-export([files_count/1]).

files_count(Dir) ->
    case file:list_dir(Dir) of  
         {ok, FileNames} ->
            length(FileNames);
        {error, Reason} ->
            Reason
    end.