1

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

1
  • It listed the current directory . and the previous directory shortcuts .., but nothing else. Are you sure the system you're on is able to read the C:/ partition correctly? Commented Aug 24, 2011 at 12:23

3 Answers 3

3

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

Dir['c:/windows/*/']
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

Are you sure? if -d i is not even valid Ruby.
not sure at all now as i've tried it. hmm, i'm almost certain i've used -f -e -d etc with files, but maybe i've been coding in perl
(previous comment was referring to earlier version of answer)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.