[erlang-questions] Retrieving priv directory path

Håkan Mattsson <>
Wed Oct 26 10:37:56 CEST 2011


On Wed, Oct 26, 2011 at 6:50 AM, Garrett Smith <> wrote:
> Hi Nikola,
>
> On Tue, Oct 25, 2011 at 6:42 PM, Nikola Skoric <> wrote:
>> What would be universal way to get priv dir path?
>>
>> Currently I retrieve priv dir like this:
>>
>> priv_dir(App) ->
>>    case code:priv_dir(App) of
>>        {error, bad_name} ->
>>            {ok, Cwd} = file:get_cwd(),
>>            Cwd ++ "/" ++ "priv/";
>>        Priv ->
>>            Priv ++ "/"
>>    end.
>>
>> First case is when I develop code, it does not work when I make a
>> release. When I make a release, core:priv_dir returns {error,
>> bad_name} because application:get_application returns something that
>> core:priv_dir doesn't agree with.
>>
>> So, how do you do it?
>
> I use this:
>
> priv_dir(Mod) ->
>    Ebin = filename:dirname(code:which(Mod)),
>    filename:join(filename:dirname(Ebin), "priv").

Keep in mind that this trick not necessarily will work if you are putting
parts of your application in an archive. In that case you need to use
code:priv_dir/1 or code:lib_dir/2 in order to find the correct priv dir.

/Håkan



More information about the erlang-questions mailing list