[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to get special directories?

Niklas Baumstark

7/31/2008 2:58:00 PM

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
--
Posted via http://www.ruby-....

17 Answers

Shashank Agarwal

7/31/2008 4:30:00 PM

0

Niklas Baumstark wrote:
> 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.
--
Posted via http://www.ruby-....

Dave Bass

7/31/2008 4:33:00 PM

0

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

p ENV

to see what's available on your OS.
--
Posted via http://www.ruby-....

Niklas Baumstark

7/31/2008 4:36:00 PM

0

> "/" 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
--
Posted via http://www.ruby-....

Niklas Baumstark

7/31/2008 4:41:00 PM

0

Dave Bass wrote:
> 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
--
Posted via http://www.ruby-....

Sebastian Hungerecker

7/31/2008 4:41:00 PM

0

Dave Bass wrote:
> 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.

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Eric Hodel

7/31/2008 5:10:00 PM

0

On Jul 31, 2008, at 07:57 AM, Niklas Baumstark wrote:
> 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.

Niklas Baumstark

7/31/2008 5:14:00 PM

0

Eric Hodel wrote:
> On Jul 31, 2008, at 07:57 AM, Niklas Baumstark wrote:
>> 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.

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
--
Posted via http://www.ruby-....

Gordon Thiesfeld

7/31/2008 5:26:00 PM

0

On Thu, Jul 31, 2008 at 12:13 PM, Niklas Baumstark
<niklas.baumstark@gmail.com> wrote:
> Eric Hodel wrote:
>> On Jul 31, 2008, at 07:57 AM, Niklas Baumstark wrote:
>>> 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.

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
>> require 'win32/dir'
=> true
>> Dir::PROFILE
=> "C:\\Documents and Settings\\gthiesfeld"
>>

Niklas Baumstark

7/31/2008 5:29:00 PM

0

Gordon Thiesfeld wrote:
> On Thu, Jul 31, 2008 at 12:13 PM, Niklas Baumstark
> <niklas.baumstark@gmail.com> wrote:
>> Eric Hodel wrote:
>>> On Jul 31, 2008, at 07:57 AM, Niklas Baumstark wrote:
>>>> 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.
>
> 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
>>> require 'win32/dir'
> => true
>>> Dir::PROFILE
> => "C:\\Documents and Settings\\gthiesfeld"

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

Greetings,
Niklas
--
Posted via http://www.ruby-....

Frederick Cheung

7/31/2008 6:24:00 PM

0


On 31 Jul 2008, at 18:29, Niklas Baumstark wrote:

> Gordon Thiesfeld wrote:
>> On Thu, Jul 31, 2008 at 12:13 PM, Niklas Baumstark
>> <niklas.baumstark@gmail.com> wrote:
>>> Eric Hodel wrote:
>>>> On Jul 31, 2008, at 07:57 AM, Niklas Baumstark wrote:
>>>>> 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.
>>
>> 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
>>>> require 'win32/dir'
>> => true
>>>> Dir::PROFILE
>> => "C:\\Documents and Settings\\gthiesfeld"
>
> 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