[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

checking for directory (noob mistake?

bilal33

7/24/2006 3:53:00 AM

I apologize in advance if this is a stupid question, but this is the
first time I'm playing around with ruby and I'm kinda stuck. Been going
thru the pickaxe as well, but maybe I'm just missing something.

I have the following code

Dir.foreach("c:/") do |temp|
p "#{temp} #{File.expand_path temp}"
p File.directory? "#{File.expand_path temp}"
end

basically, I need to detect which items under my C drive (or any other
folder for that matter) are directories. With the code above, I can't
seem to get this to work. I added the expand path call so that I know
that I am getting the folders correctly.

Am I missing something really obvious here? Why can't I differentiate
between a file and a directory?

I'm using Ruby 184-19 (the one click installer).

Thanks

3 Answers

Daniel Harple

7/24/2006 4:17:00 AM

0

On Jul 23, 2006, at 11:55 PM, bilal33@gmail.com wrote:

> Dir.foreach("c:/") do |temp|
> p "#{temp} #{File.expand_path temp}"
> p File.directory? "#{File.expand_path temp}"
> end
>
> basically, I need to detect which items under my C drive (or any other
> folder for that matter) are directories. With the code above, I can't
> seem to get this to work. I added the expand path call so that I know
> that I am getting the folders correctly.
>
> Am I missing something really obvious here? Why can't I differentiate
> between a file and a directory?

Dir::entries (which Dir::foreach uses) just returns a list of files/
directories in the specified path, it does not return the path to
them. You want Dir::glob:

Dir.glob("/*") do |dir|
puts "#{dir}: directory? #{File.directory?(dir)}"
end

-- Daniel


Kev Jackson

7/24/2006 4:18:00 AM

0

I'm not sure, but works fine on my machine (OSX)

Dir.foreach("/Users/kj") do |temp|
p "#{temp} #{File.expand_path temp}"
p File.directory? "#{File.expand_path temp}"
end

...

Spikefish:~/projects/ant-tla/trunk kj$ ruby ~/test.rb
/Users/kj/test.rb:3: warning: parenthesize argument(s) for future
version
". /Users/kj/projects/ant-tla/trunk"
true
".. /Users/kj/projects/ant-tla"
true
"-private- /Users/kj/projects/ant-tla/trunk/-private-"
false
".appletviewer /Users/kj/projects/ant-tla/trunk/.appletviewer"
false


--
"In vain you tell me that Artificial Government is good, but that I
fall out with the Abuse. The Thing! The Thing itself is the Abuse!" -
Edmund Burke


Peña, Botp

7/24/2006 4:47:00 AM

0

fr Kev:
# I'm not sure, but works fine on my machine (OSX)
# ...
# Spikefish:~/projects/ant-tla/trunk kj$ ruby ~/test.rb
# /Users/kj/test.rb:3: warning: parenthesize argument(s) for future
# version
# ". /Users/kj/projects/ant-tla/trunk"
# true
# ".. /Users/kj/projects/ant-tla"
# true
# "-private- /Users/kj/projects/ant-tla/trunk/-private-"
# false
# ".appletviewer /Users/kj/projects/ant-tla/trunk/.appletviewer"
# false

careful. it seems to work since you are sitting on it. read further on expand_path...

eg,

irb(main):009:0> puts `pwd`
/home/botp
=> nil
irb(main):010:0> Dir.foreach("/home/botp") do |temp|
irb(main):011:1* p "#{temp} #{File.expand_path temp}"
irb(main):012:1> p File.directory? "#{File.expand_path temp}"
irb(main):013:1> end
(irb):12: warning: parenthesize argument(s) for future version
". /home/botp"
true
".. /home"
true
".w3m /home/botp/.w3m"
true
"installer /home/botp/installer"
true
"mbox /home/botp/mbox"
false
"mail /home/botp/mail"
true
....

irb(main):009:0> puts `pwd`
/etc
=> nil
irb(main):010:0> Dir.foreach("/home/botp") do |temp|
irb(main):011:1* p "#{temp} #{File.expand_path temp}"
irb(main):012:1> p File.directory? "#{File.expand_path temp}"
irb(main):013:1> end
(irb):12: warning: parenthesize argument(s) for future version
". /etc"
true
".. /"
true
".w3m /etc/.w3m"
false
"installer /etc/installer"
false
"mbox /etc/mbox"
false
"mail /etc/mail"
false

maybe we may want foreach to optionally pass also the path, like so

Dir.foreach("/home/botp") do |temp,path|
p File.expand_path(temp,path)
end

of course, there is Dir.glob :)