Home Directory On Windows

82 views
Skip to first unread message

Onorio Catenacci

unread,
May 6, 2016, 12:15:48 PM5/6/16
to elixir-lang-core
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,_} -> "~"
  end


You can see the issue in this way:

1.) Fire up iex

2.) iex(1)> File.mkdir_p!("~/bogus")
:ok

3.) PS C:\Users\ocatenacci> dir b*


    Directory: C:\Users\ocatenacci


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        7/16/2015   3:14 PM                Babou
-a----       11/20/2015   3:39 PM           7606 boot.bat

4.) iex(2)> home = System.get_env("UserProfile")
"C:\\Users\\ocatenacci"
iex(3)> File.mkdir_p!("#{home}/bogus")
:ok

5.) PS C:\Users\ocatenacci> dir b*


    Directory: C:\Users\ocatenacci


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        7/16/2015   3:14 PM                Babou
d-----         5/6/2016  12:12 PM                bogus
-a----       11/20/2015   3:39 PM           7606 boot.bat


I hadn't actually checked this on Unix but I'd be shocked were it a problem.  The only real annoyance to this is that no error is thrown--the directory simply isn't created.  
I can file an issue if it's wanted.  Just wanted to make sure those few of us who use windows are aware of the issue.

--
Onorio


Onorio Catenacci

unread,
May 6, 2016, 12:45:16 PM5/6/16
to elixir-lang-core
And, of course, after I post that, I spot System.user_home/0. 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.

--
Onorio

José Valim

unread,
May 6, 2016, 12:50:13 PM5/6/16
to elixir-l...@googlegroups.com
The behavior you described is also the behavior on UNIX. You must expand paths with Path.expand if you want entries like "~" to be expanded.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/8f4c4c17-a327-40a5-8928-318e8641341f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--


José Valim
Skype: jv.ptec
Founder and Director of R&D

Onorio Catenacci

unread,
May 6, 2016, 1:28:01 PM5/6/16
to elixir-lang-core
Oh--that's good to know José.  I had just assumed that Unix wouldn't have that issue since Windows seems to be such a fertile hunting ground for odd behaviors. 


--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/g22tkekQcMo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4JQiA0PnfZoJT6ZF5ZgmOVUZ%3Djkp1_0Qrs93%2B_MRwuqag%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

Dmitry Belyaev

unread,
May 12, 2016, 4:26:03 AM5/12/16
to elixir-l...@googlegroups.com
The feature of expanding ~ belongs to bash, not Linux itself.
--
Best wishes,
Dmitry Belyaev

Onorio Catenacci

unread,
May 12, 2016, 1:51:24 PM5/12/16
to elixir-lang-core
Thanks for the clarification @Dmitry.  I tend to conflate bash and Linux which is a mistake of course.

--
Onorio
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.


--


José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/g22tkekQcMo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4JQiA0PnfZoJT6ZF5ZgmOVUZ%3Djkp1_0Qrs93%2B_MRwuqag%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages