12 messages in this thread from ruby-talk in 2004-02
Is there a way to specify a directory in Ruby that is profile-independent
for the Windows NT variants? For example, if I wanted to do this in VBA for
the currently logged in user's My Documents folder, it would look like this:
ProfilePath = Environ("USERPROFILE") & "\My Documents\"
This way, regardless of who is logged in and runs the script, it will always
point to their own My Documents folder. Thanks in advance for the help-
Craig
The environment is available under the hash ENV. So, you can do something like: ProfilePath = ENV["USERPROFILE"] + "\\My Documents\\" basically like you said you'd do with VBA.
On Wed, 25 Feb 2004 22:03:51 +0900, Moran, Craig M (BAH) wrote:
> Is there a way to specify a directory in Ruby that is profile-independent
> for the Windows NT variants? For example, if I wanted to do this in VBA
> for the currently logged in user's My Documents folder, it would look
> like this: ProfilePath = Environ("USERPROFILE") & "\My Documents\"
> This way, regardless of who is logged in and runs the script, it will
> always point to their own My Documents folder. Thanks in advance for the
> help- Craig
profile_path = "#{ENV['USERPROFILE']}\\My Documents\\"
-austin
--
austin ziegler * austin@ha...ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2004.02.25
* 11.08.25
Thanks! This works perfectly. However, Austin's solution does not. I'd imagine it was a misplaced quote. Is either style preferable over the other? Craig
-----Original Message----- From: Jim Bob [mailto:none@in...com] Sent: Wednesday, February 25, 2004 11:00 AM To: ruby-talk@ru...org Subject: Re: Profile-independent directory specification for NT? The environment is available under the hash ENV. So, you can do something like: ProfilePath = ENV["USERPROFILE"] + "\\My Documents\\" basically like you said you'd do with VBA.
On Thu, 26 Feb 2004 01:38:25 +0900, Moran, Craig M (BAH) wrote: > Thanks! This works perfectly. However, Austin's solution does not. I'd > imagine it was a misplaced quote. Is either style preferable over the > other? Craig
I just tried my version in Windows XP with irb and it produced the exact
result.
With the version provided by Jim Bob, you get a string, create a string, and
then add them to a new string. With mine, you're only creating one string
through.
irb(main):030:0> profile_path = "#{ENV['USERPROFILE']}\\My Documents\\"
=> "C:\\Documents and Settings\\Austin\\My Documents\\"
-austin
--
austin ziegler * austin@ha...ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2004.02.25
* 12.21.29
My bad. I did #ENV{ instead of #{ENV. It behaves correctly now. I
appreciate the explanation.
Craig
-----Original Message-----
From: Austin Ziegler [mailto:austin@ha...ca]
Sent: Wednesday, February 25, 2004 12:27 PM
To: ruby-talk@ru...org
Subject: Re: Profile-independent directory specification for NT?
On Thu, 26 Feb 2004 01:38:25 +0900, Moran, Craig M (BAH) wrote:
> Thanks! This works perfectly. However, Austin's solution does not. I'd
> imagine it was a misplaced quote. Is either style preferable over the
> other? Craig
I just tried my version in Windows XP with irb and it produced the exact
result.
With the version provided by Jim Bob, you get a string, create a string, and
then add them to a new string. With mine, you're only creating one string
through.
irb(main):030:0> profile_path = "#{ENV['USERPROFILE']}\\My Documents\\"
=> "C:\\Documents and Settings\\Austin\\My Documents\\"
-austin
--
austin ziegler * austin@ha...ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2004.02.25
* 12.21.29
On Wed, Feb 25, 2004 at 10:03:51PM +0900, Moran, Craig M (BAH) wrote:
> Is there a way to specify a directory in Ruby that is profile-independent
> for the Windows NT variants? For example, if I wanted to do this in VBA for
> the currently logged in user's My Documents folder, it would look like this:
> ProfilePath = Environ("USERPROFILE") & "\My Documents\"
> This way, regardless of who is logged in and runs the script, it will always
> point to their own My Documents folder. Thanks in advance for the help-Be aware that this is only correct on english versions of windows. In german e.g, the folder is not named "My Documents" but "Meine Dateien" (or something like that - don't use windows very often ;-) ). I believe that the windows registry is the right place to get that information from. (I have to admit though, that I have no idea how you could access the registry from ruby, and where you find this information there). greetings, Florian Pflug
"Florian G. Pflug" <fgp@ph...org> wrote in message news:<20040225194848.GA24254@fo...com>...
> On Wed, Feb 25, 2004 at 10:03:51PM +0900, Moran, Craig M (BAH) wrote:
> > Is there a way to specify a directory in Ruby that is profile-independent
> > for the Windows NT variants? For example, if I wanted to do this in VBA for
> > the currently logged in user's My Documents folder, it would look like this:
> > ProfilePath = Environ("USERPROFILE") & "\My Documents\"
> > This way, regardless of who is logged in and runs the script, it will always
> > point to their own My Documents folder. Thanks in advance for the help-
> Be aware that this is only correct on english versions of windows. In german
> e.g, the folder is not named "My Documents" but "Meine Dateien" (or
> something like that - don't use windows very often ;-) ).
In theory, each user has a home directory set. You can see if it's
set by going to Start -> Settings -> Control Panel -> Users and
Passwords -> Advanced -> Advanced (again) -> Click on users -> right
click to view properties -> click "Profile" tab.
You can use the win32-etc module to get at this information as well in
a language-neutral way:
require "win32/etc"
include Win32
Etc.passwd{ |s|
p s.name
p s.home_dir
}
Often times, however, this value isn't set and you have to resort to
environment variables.
> I believe that the windows registry is the right place to get that > information from. (I have to admit though, that I have no idea how you could access > the registry from ruby, and where you find this information there). > > greetings, Florian Pflug
You can use "win32/registry" which ships with 1.8.1. However, there's no advantage to using the registry over the environment variable since the two are identical as far as I know. Regards, Dan
Florian G. Pflug wrote:
> On Wed, Feb 25, 2004 at 10:03:51PM +0900, Moran, Craig M (BAH) wrote:
>
>>Is there a way to specify a directory in Ruby that is profile-independent
>>for the Windows NT variants? For example, if I wanted to do this in VBA for
>>the currently logged in user's My Documents folder, it would look like this:
>>ProfilePath = Environ("USERPROFILE") & "\My Documents\"
>>This way, regardless of who is logged in and runs the script, it will always
>>point to their own My Documents folder. Thanks in advance for the help-
>
> Be aware that this is only correct on english versions of windows. In german
> e.g, the folder is not named "My Documents" but "Meine Dateien" (or
> something like that - don't use windows very often ;-) ).
>
> I believe that the windows registry is the right place to get that
> information from. (I have to admit though, that I have no idea how you could access
> the registry from ruby, and where you find this information there).
>
> greetings, Florian PflugIn Win2K and up you can call a Shell function SHGetFolderPath( ) which can be used to return this directory. for example, SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, 0, szPath) Alternatively, you can look in the registry in the following branch: "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" Under Win2K and up, there will be an entry labelled "Personal" which contains this directory name.
Hi, In message <20040225194848.GA24254@fo...com>,
"Florian G. Pflug" <fgp@ph...org> wrote:
>On Wed, Feb 25, 2004 at 10:03:51PM +0900, Moran, Craig M (BAH) wrote:
>> Is there a way to specify a directory in Ruby that is profile-independent
>> for the Windows NT variants? For example, if I wanted to do this in VBA for
>> the currently logged in user's My Documents folder, it would look like this:
>> ProfilePath = Environ("USERPROFILE") & "\My Documents\"
>> This way, regardless of who is logged in and runs the script, it will always
>> point to their own My Documents folder. Thanks in advance for the help-
>Be aware that this is only correct on english versions of windows. In german
>e.g, the folder is not named "My Documents" but "Meine Dateien" (or
>something like that - don't use windows very often ;-) ).
>
>I believe that the windows registry is the right place to get that
>information from. (I have to admit though, that I have no idea how you could access
>the registry from ruby, and where you find this information there).
Using Windows Scripting Host, you can do:
require 'win32ole'
shell = WIN32OLE.new("WScript.Shell")
ProfilePath = shell.specialFolders("MyDocuments")
--
KUSUNOSE Toru mailto:kusunose@hc...jp
KUSUNOSE Toru <kusunose@hc...jp> wrote:
> Using Windows Scripting Host, you can do:
>
> require 'win32ole'
> shell = WIN32OLE.new("WScript.Shell")
> ProfilePath = shell.specialFolders("MyDocuments")
Alternatively you can use the explorer's "Shell" object:
WIN32OLE.new('Shell.application')
Access to the path is not as easy, though. You need to know some
constants which can be found in the Windows SDK. The good thing about
using those constants is that they are language independent.
Play around with the code at
http://people.freenet.de/wickie/winshell.zip
Cheers
Sascha
If you want a profile _independent_ path, then just use an absolute
pathname.
I think that you want a profile _dependent_ path. The most correct
way to get one is with WMI. You will have to peruse the sdk docs to
determine the correct way of finding this pathname for your network
setup, though.
A slightly simpler method that you might try:
require 'win32ole'
wsh = WIN32OLE.new("WScript.Shell")
desktop = wsh.SpecialFolders("Desktop")
puts "desktop = '#{desktop}'"
This assumes wsh availability (present if you use vba on this
machine).
If you come from a vba background, you should probably get ArtonX's
excellent activerubyscript package from
http://www.geocities.co.jp/SiliconValley-PaloAlto/9251/ruby/main.html
.. It has a wsh<->ruby bridge that lets you use ruby via wsh in ie,
iis, word, and wherever else you would normally use vba. It should
also lower the bar for translating vba to ruby. It is a shame that
the base windows installs don't provide this functionality, since it
is de rigour for all modern scripting languages (I could be wrong
about its inclusion, but http://rubyinstaller.sourceforge.net tells
you to look at http://rubygarden.org/ruby?WindowsInstaller for a
manifest and vice-versa and I don't have the patience for such
nonsense).
"Moran, Craig M (BAH)" <MoranCM@na...mil> wrote in message news:<B9500E218FB4D2119DE30000F810BBBF0F1176A9@ne...mil>...
> Is there a way to specify a directory in Ruby that is profile-independent
> for the Windows NT variants? For example, if I wanted to do this in VBA for
> the currently logged in user's My Documents folder, it would look like this:
> ProfilePath = Environ("USERPROFILE") & "\My Documents\"
> This way, regardless of who is logged in and runs the script, it will always
> point to their own My Documents folder. Thanks in advance for the help-> Craig