Home Directory On Windows

Quick Pro Tip:

Apparently on Windows, the “~” to refer to the home directory doesn’t work correctly. And it doesn’t throw any sort of error to let you know that it doesn’t work correctly so it will simply silently fail. Here’s a bit of code I just hacked together to work around this issue (FWIW):

home_dir =
  case (:os.type()) do
    {:win32,_} -> System.get_env("UserProfile")
    {:unix,_} -> "~"

Hope this may help someone else avoid wasting time.


And, of course, after I post that, I spot System.user_home/0. :slight_smile: So this would be even better yet:

home_dir = System.user_home

I guess the upshot is that you can’t rely on “~” on Windows. I don’t mind that it doesn’t work; it’s the fact that it doesn’t give any indication that it won’t work that bothers me.