# File.expand\_path and ~ on windows

**URL:** https://rubytalk.org/t/file-expand-path-and-on-windows/8134
**Category:** ruby-talk
**Created:** [18 September 2003 16:34 UTC](https://rubytalk.org/t/file-expand-path-and-on-windows/8134 "2003-09-18T16:34:53Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Alan\_Davies1](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@Alan\_Davies1](https://rubytalk.org/u/Alan_Davies1)
#### Post date: [18 September 2003 16:34 UTC](https://rubytalk.org/t/file-expand-path-and-on-windows/8134/1 "2003-09-18T16:34:53Z")

</div>

From the pickaxe book…

> File.expand\_path( fileName [, aDirString] ) → fileName  
>   
> The given pathname may start with a ``~‘’, which expands to the  
> process owner’s home directory (the environment variable HOME must be  
> set correctly). “~ user” expands to the named user’s home directory"

This isn’t relevant on windows. Thus:

File.expand\_path(“~foo”, “c:/somedir”) → “~foo”

where in fact it should return “c:/somedir/~foo”.

Also:

File.expand\_path(“~”, “c:/somedir”)

throws up the error “`expand_path': couldn't find HOME environment -- expanding `~’ (ArgumentError)”

It seems strange that the first example doesn’t throw up the same error  
that the second one does.

Can this functionality be disbaled on windows platforms in a future  
version? Or at least add a parameter to the function call to disable it?

Alan.

---

<div class="post-metadata">

### Author: ![Nucleon](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nucleon](https://rubytalk.org/u/Nucleon)
#### Post date: [19 September 2003 02:36 UTC](https://rubytalk.org/t/file-expand-path-and-on-windows/8134/2 "2003-09-19T02:36:28Z")

</div>

Wouldn’t it make more sense to expand it to My Documents (C:\Documents and  
Settings\Username\My Documents on WinXP)? I suppose it would be  
worthwhile to be able to disable it, though.

> **···**
>
> On Thu, 18 Sep 2003 17:16:00 +0100, Alan Davies wrote:
> 
> > From the pickaxe book…
> > 
> > > File.expand\_path( fileName [, aDirString] ) → fileName  
> > >   
> > > The given pathname may start with a ``~‘’, which expands to the  
> > > process owner’s home directory (the environment variable HOME must be  
> > > set correctly). “~ user” expands to the named user’s home directory"
> > 
> > This isn’t relevant on windows. Thus:
> 
> –  
> Tom Felker, [tcfelker@mtco.com](mailto:tcfelker@mtco.com)  
> [http://vlevel.sourceforge.net](http://vlevel.sourceforge.net) - Stop fiddling with the volume knob.
> 
> You both prefer a universe in which the other party hasn’t magically  
> disappeared. I think we have a framework for peace.

---

<div class="post-metadata">

### Author: ![Austin\_Ziegler2](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Austin\_Ziegler2](https://rubytalk.org/u/Austin_Ziegler2)
#### Post date: [19 September 2003 03:14 UTC](https://rubytalk.org/t/file-expand-path-and-on-windows/8134/3 "2003-09-19T03:14:32Z")

</div>

No. ~ has no meaning to Windows shells. There are times when I wish that it  
didn’t to Unix shells. After all, that’s what ${HOME} is for.

-austin

> **···**
>
> On Fri, 19 Sep 2003 11:36:28 +0900, Tom Felker wrote:
> 
> > Wouldn’t it make more sense to expand it to My Documents (C:\Documents  
> > and Settings\Username\My Documents on WinXP)? I suppose it would be  
> > worthwhile to be able to disable it, though.
> 
> –  
> austin ziegler \* austin@halostatue.ca \* Toronto, ON, Canada  
> software designer \* pragmatic programmer \* 2003.09.18  
> \* 23.12.23

---

<div class="post-metadata">

### Author: ![\_Sean\_O\_Dell](https://avatars.discourse-cdn.com/v4/letter/_/4491bb/32.png) [@\_Sean\_O\_Dell](https://rubytalk.org/u/_Sean_O_Dell)
#### Post date: [19 September 2003 16:18 UTC](https://rubytalk.org/t/file-expand-path-and-on-windows/8134/4 "2003-09-19T16:18:33Z")

</div>

Tom Felker wrote:

> **···**
>
> > On Thu, 18 Sep 2003 17:16:00 +0100, Alan Davies wrote:
> > 
> > > From the pickaxe book…
> > > 
> > > > File.expand\_path( fileName [, aDirString] ) → fileName  
> > > >   
> > > > The given pathname may start with a ``~‘’, which expands to the  
> > > > process owner’s home directory (the environment variable HOME must be  
> > > > set correctly). “~ user” expands to the named user’s home directory"
> > > 
> > > This isn’t relevant on windows. Thus:
> > 
> > Wouldn’t it make more sense to expand it to My Documents (C:\Documents and  
> > Settings\Username\My Documents on WinXP)? I suppose it would be  
> > worthwhile to be able to disable it, though.
> 
> Actually, I think it should just expand to C:\Documents and  
> Settings\UserName and you can append the rest from there. That’s the  
> \*nix equivalent pretty much.
> 
> ```
> Sean O'Dell
> 
> ```

---

<div class="post-metadata">

### Author: ![\_Sean\_O\_Dell](https://avatars.discourse-cdn.com/v4/letter/_/4491bb/32.png) [@\_Sean\_O\_Dell](https://rubytalk.org/u/_Sean_O_Dell)
#### Post date: [19 September 2003 16:18 UTC](https://rubytalk.org/t/file-expand-path-and-on-windows/8134/5 "2003-09-19T16:18:35Z")

</div>

Austin Ziegler wrote:

> **···**
>
> > On Fri, 19 Sep 2003 11:36:28 +0900, Tom Felker wrote:
> > 
> > > Wouldn’t it make more sense to expand it to My Documents (C:\Documents  
> > > and Settings\Username\My Documents on WinXP)? I suppose it would be  
> > > worthwhile to be able to disable it, though.
> > 
> > No. ~ has no meaning to Windows shells. There are times when I wish that it  
> > didn’t to Unix shells. After all, that’s what ${HOME} is for.
> 
> This is true; but I think for portability reasons, since [COMMAND.COM](http://COMMAND.COM)  
> isn’t exactly what I would call a culture of shell idioms, ~ and ${HOME}  
> (%HOME%) should be considered synonymous. If not ~, then at least  
> ${HOME} ought to point to C:\Documents and Settings\UserName I think.
> 
> ```
> Sean O'Dell
> 
> ```

---

<div class="post-metadata">

### Author: ![Alan\_Davies1](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@Alan\_Davies1](https://rubytalk.org/u/Alan_Davies1)
#### Post date: [22 September 2003 16:30 UTC](https://rubytalk.org/t/file-expand-path-and-on-windows/8134/6 "2003-09-22T16:30:22Z")

</div>

Sean O’Dell wrote:

> Tom Felker wrote:
> 
> > > From the pickaxe book…
> > > 
> > > > File.expand\_path( fileName [, aDirString] ) → fileName  
> > > >   
> > > > The given pathname may start with a ``~‘’, which expands to the  
> > > > process owner’s home directory (the environment variable HOME must be  
> > > > set correctly). “~ user” expands to the named user’s home directory"
> > > 
> > > This isn’t relevant on windows. Thus:
> > 
> > Wouldn’t it make more sense to expand it to My Documents (C:\Documents  
> > and  
> > Settings\Username\My Documents on WinXP)? I suppose it would be  
> > worthwhile to be able to disable it, though.
> 
> Actually, I think it should just expand to C:\Documents and  
> Settings\UserName and you can append the rest from there. That’s the  
> \*nix equivalent pretty much.
> 
> ```
> Sean O'Dell
> 
> ```

Some interesting points being made on this thread, but the problem I had  
was that I has a files beginning with ~ in my temp folder. Using  
expand\_path to get the full path of these files failed. If it expanded  
it to user directories, then it woulod be returning incrrect path names,  
simply because ~ has no meaning on windows.

If I have a file called ~foo in a dir called “c:/somedir”, then I would  
expand\_path to return “c:/somedir/~foo”, not “C:/Documents and Settings/foo”

Alan.

> **···**
>
> > > On Thu, 18 Sep 2003 17:16:00 +0100, Alan Davies wrote:

---

<div class="post-metadata">

### Author: ![Austin\_Ziegler2](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Austin\_Ziegler2](https://rubytalk.org/u/Austin_Ziegler2)
#### Post date: [19 September 2003 17:27 UTC](https://rubytalk.org/t/file-expand-path-and-on-windows/8134/7 "2003-09-19T17:27:39Z")

</div>

That’s a no fly zone, as far as I’m concerned. My %HOME% is not the same as  
my %USERPROFILE%. Further, ~foo is a valid filename on Windows. (It’s valid  
in Unix, too, but you have to do ~foo to get it to work from the shell.)

I’d like to see Ruby handle this right on each platform.

-austin

> **···**
>
> On Sat, 20 Sep 2003 01:18:35 +0900, Sean O’Dell wrote:
> 
> > Austin Ziegler wrote:
> > 
> > > On Fri, 19 Sep 2003 11:36:28 +0900, Tom Felker wrote:
> > > 
> > > > Wouldn’t it make more sense to expand it to My Documents  
> > > > (C:\Documents and Settings\Username\My Documents on WinXP)? I  
> > > > suppose it would be worthwhile to be able to disable it, though.  
> > > > No. ~ has no meaning to Windows shells. There are times when I wish  
> > > > that it didn’t to Unix shells. After all, that’s what ${HOME} is for.  
> > > > This is true; but I think for portability reasons, since [COMMAND.COM](http://COMMAND.COM)  
> > > > isn’t exactly what I would call a culture of shell idioms, ~ and ${HOME}  
> > > > (%HOME%) should be considered synonymous. If not ~, then at least  
> > > > ${HOME} ought to point to C:\Documents and Settings\UserName I think.
> 
> –  
> austin ziegler \* austin@halostatue.ca \* Toronto, ON, Canada  
> software designer \* pragmatic programmer \* 2003.09.19  
> \* 13.23.38

---

<div class="post-metadata">

### Author: ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)
#### Post date: [20 September 2003 00:57 UTC](https://rubytalk.org/t/file-expand-path-and-on-windows/8134/8 "2003-09-20T00:57:40Z")

</div>

I use Windows, and choose to set HOME to D:/home/gavin, so that  
Windows and Cygwin can be in agreement. Certain Windows programs will  
make use of any %HOME% setting.

Gavin

> **···**
>
> On Saturday, September 20, 2003, 2:18:35 AM, Sean wrote:
> 
> > Austin Ziegler wrote:
> 
> > > On Fri, 19 Sep 2003 11:36:28 +0900, Tom Felker wrote:
> > > 
> > > > Wouldn’t it make more sense to expand it to My Documents (C:\Documents  
> > > > and Settings\Username\My Documents on WinXP)? I suppose it would be  
> > > > worthwhile to be able to disable it, though.
> > > 
> > > No. ~ has no meaning to Windows shells. There are times when I wish that it  
> > > didn’t to Unix shells. After all, that’s what ${HOME} is for.
> 
> > This is true; but I think for portability reasons, since [COMMAND.COM](http://COMMAND.COM)  
> > isn’t exactly what I would call a culture of shell idioms, ~ and ${HOME}  
> > (%HOME%) should be considered synonymous. If not ~, then at least  
> > ${HOME} ought to point to C:\Documents and Settings\UserName I think.
