Profile-independent directory specification for NT?

  • Thread starter Moran, Craig M (BAH)
  • Start date
M

Moran, Craig M (BAH)

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
 
J

Jim Bob

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.
 
F

Florian G. Pflug

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
 
D

Daniel Berger

Florian G. Pflug said:
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
 
E

eg

Florian said:
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

In 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.
 
K

KUSUNOSE Toru

Hi,

In message said:
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")
 
A

anon luker

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).
 
S

Sascha D?rdelmann

KUSUNOSE Toru said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top