How to get special directories?

N

Niklas Baumstark

hi all,

how can i get ruby to get me some special directories, such as the
home-folder on linux or "documents and settings" on windows? is there
already some functionality available to do this or will i have to use
platform-native functions (for example COM on windows)?

Greetings,
Niklas
 
S

Shashank Agarwal

Niklas said:
hi all,

how can i get ruby to get me some special directories, such as the
home-folder on linux or "documents and settings" on windows? is there
already some functionality available to do this or will i have to use
platform-native functions (for example COM on windows)?

Greetings,
Niklas

I'm not sure if this'll work, but "/" should get you the home folder in
linux, and "C:/Documents and Settings" should work fine on Windows.
 
D

Dave Bass

Have you looked at Ruby's ENV environment hash? Try:

p ENV

to see what's available on your OS.
 
N

Niklas Baumstark

"/" should get you the home folder in linux

surely it should not.
and "C:/Documents and Settings" should work fine on Windows.

well, my home dir is under D:/home...

but you're right, i could use File.expand_path("~") under linux. i could
maybe use COM under windows and use the scripting host to get the
directory. but this isn't very elegant. is there no better way? if there
is none: is it possible to access COM from ruby?

Greetings,
Niklas
 
N

Niklas Baumstark

Dave said:
Have you looked at Ruby's ENV environment hash? Try:

p ENV

to see what's available on your OS.

sry, haven't seen this before posting. the problem is, that it's an app
that is shared between linux, windows and maybe OS X machines, so it
must be cross-platform. and ENV is not globally available. i've had the
sitation recently, that a SVN post-commit hook-script didn't inherit its
variables from the environment so the ruby script it called didn't have
any environment variables to use.

Greetings,
Niklas
 
S

Sebastian Hungerecker

Dave said:
Have you looked at Ruby's ENV environment hash?

Not that it makes any kind of a difference, but ENV isn't actually a hash.
Just thought I'd mention that for correctness' sake.
 
E

Eric Hodel

how can i get ruby to get me some special directories, such as the
home-folder on linux or "documents and settings" on windows? is there
already some functionality available to do this or will i have to use
platform-native functions (for example COM on windows)?

To get folders like 'documents and settings' on windows you need to
use win32api. Depending on language, these may have different names.
 
N

Niklas Baumstark

Eric said:
To get folders like 'documents and settings' on windows you need to
use win32api. Depending on language, these may have different names.

okay so i have to make a platform switch... is it possible that you
could give an example of what API-Call to do and how?

Greetings,
Niklas
 
G

Gordon Thiesfeld

You can also use the win32-dir gem

C:\>gem in win32-dir
Successfully installed win32-dir-0.3.2
1 gem installed
C:\>irb
 
N

Niklas Baumstark

Gordon said:
You can also use the win32-dir gem

C:\>gem in win32-dir
Successfully installed win32-dir-0.3.2
1 gem installed
C:\>irb
=> "C:\\Documents and Settings\\gthiesfeld"

Thanks that'll help alot! Is something similiar available on linux as
well?

Greetings,
Niklas
 
F

Frederick Cheung

Thanks that'll help alot! Is something similiar available on linux as
well?
Dunno about linux, but here's something that will work on mac os x.
The way mac os x tracks things like disk is a combination of a domain
and a folder type.
Domains are thinks like user domain, local domain etc..., folder types
are things like 'applications folder', 'library folder' and so on.
So (in C) the user's library folder (/Users/fred/Library in my case)
is identified by kUserDomain, kDomainLibraryFolderType and the top
level one (/Library) is identified by
kLocalDomain, kDomainLibraryFolderType

The following code uses rubyinline to call some C code. This doesn't
seem to work if you paste it into irb, but as a standalone ruby file
runs fine

require 'rubygems'
gem 'RubyInline'
require 'inline'

class Folder
module Domains
USER = -32763
SYSTEM_DISK = -32768
SYSTEM = -32766
LOCAL = -32765
end

module Types
def self.int_for_code code
code.unpack('N').first
end

TOP = int_for_code 'dtop' #more of these constants in Folders.h
DOCUMENTS = int_for_code 'docs'
end

inline do |builder|
builder.include "<CoreServices/CoreServices.h>"
builder.add_compile_flags "-framework CoreServices"
builder.c_singleton "
static char *find(int domain, unsigned long folder_type){
char c_path[PATH_MAX];
FSRef folder;
if(noErr == FSFindFolder(domain, folder_type, true, &folder)){
if(noErr ==FSRefMakePath(&folder, (UInt8*)c_path, PATH_MAX))
return c_path;
}
/* decide what you want to do in case of failure */
return \"err\";
}
"
end
end

puts Folder::find(Folder::Domains::USER, Folder::Types::DOCUMENTS)
#should output the user's documents folder
puts Folder::find(Folder::Domains::USER, Folder::Types::TOP) #should
output the top level of the user domain, ie the home folder
 
N

Nobuyoshi Nakada

Hi,

At Fri, 1 Aug 2008 01:36:20 +0900,
Niklas Baumstark wrote in [ruby-talk:309687]:
but you're right, i could use File.expand_path("~") under linux. i could
maybe use COM under windows and use the scripting host to get the
directory. but this isn't very elegant. is there no better way? if there
is none: is it possible to access COM from ruby?

It works with 1.8.7 and 1.8.6-p127 or lator, even on Windows,
and is the cross-platform and official recommended way.
 
N

Niklas Baumstark

Nobuyoshi said:
It works with 1.8.7 and 1.8.6-p127 or lator, even on Windows,
and is the cross-platform and official recommended way.

File.expand_path("~") ?

Greetings,
Niklas
 
N

Niklas Baumstark

Nobuyoshi said:
Hi,

At Fri, 1 Aug 2008 16:29:08 +0900,
Niklas Baumstark wrote in [ruby-talk:309767]:
File.expand_path("~") ?

Yes.

irb(main):001:0> File.expand_path('~')
ArgumentError: couldn't find HOME environment -- expanding `~'
from (irb):1:in `expand_path'
from (irb):1

under Windows XP Pro.

Greetings,
Niklas
 
N

Niklas Baumstark

Niklas said:
irb(main):001:0> File.expand_path('~')
ArgumentError: couldn't find HOME environment -- expanding `~'
from (irb):1:in `expand_path'
from (irb):1

under Windows XP Pro.

Greetings,
Niklas

oh sorry! got an old version of ruby:

C:\>ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

Greetings,
Niklas
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top