amir amir
amir amir

Reputation: 3435

Detecting directories with "directory?" method

I want to detects directories im my computers.but when I write below code , Ruby just give me "." and "..".

Dir.foreach("c:/windows") do |i|
   puts i if File.directory(i)
end

please help me. thanks

Upvotes: 1

Views: 122

Answers (3)

senthil raja
senthil raja

Reputation: 90

It is also the part of the directory. To move up folder. So need a conditions in the each file of the directory to remove those two files.

Upvotes: 0

Kimmo Lehto
Kimmo Lehto

Reputation: 6041

Your code seems to work fine, except File.directory(i) should be File.directory?(i)

Dir.foreach("/mnt/tmp") do |i|
   puts i if File.directory?(i)
end
=>
.
..
majic
Dropbox

Upvotes: 0

tokland
tokland

Reputation: 67860

Dir.foreach returns only the filename (relative) , so it won't work. Build the absolute path or better try a simple:

Dir['c:/windows/*/']

Upvotes: 3

Related Questions