# Checking for directory (noob mistake?)

**URL:** <https://www.ruby-forum.com/t/checking-for-directory-noob-mistake/99569>\
**Category:** Ruby\
**Created:** [July 24, 2006, 5:56am UTC](https://www.ruby-forum.com/t/checking-for-directory-noob-mistake/99569 "2006-07-24T05:56:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 24, 2006, 5:56am UTC](https://www.ruby-forum.com/t/checking-for-directory-noob-mistake/99569/1 "2006-07-24T05:56:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 24, 2006, 6:20am UTC](https://www.ruby-forum.com/t/checking-for-directory-noob-mistake/99569/2 "2006-07-24T06:20:35Z")

</div>

On Jul 23, 2006, at 11:55 PM, removed\_email\_address@domain.invalid wrote:

> 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 24, 2006, 6:20am UTC](https://www.ruby-forum.com/t/checking-for-directory-noob-mistake/99569/3 "2006-07-24T06:20:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 24, 2006, 6:48am UTC](https://www.ruby-forum.com/t/checking-for-directory-noob-mistake/99569/4 "2006-07-24T06:48:46Z")

</div>

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 🙂
