[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How distinguish between a file and a directory?

Bazsl

10/16/2007 3:01:00 PM

A newbie question. The code

Dir.foreach(Dir.getwd) do |f|
if ((f != '.') && (f != '..'))
puts f
end
end

lists the files and directories in the current working directory but
does not identify which is a file and which is a directory. How can I
add that information to the listing? Thanks.

2 Answers

Farrel Lifson

10/16/2007 3:07:00 PM

0

File::directory?(path)
will return true of name is a directory, false if it is a file

Farrel

Michael Fellinger

10/16/2007 5:19:00 PM

0

On 10/17/07, Bazsl <hs@dbginc.com> wrote:
> A newbie question. The code
>
> Dir.foreach(Dir.getwd) do |f|
> if ((f != '.') && (f != '..'))
> puts f
> end
> end
>
> lists the files and directories in the current working directory but
> does not identify which is a file and which is a directory. How can I
> add that information to the listing? Thanks.

Dir['*'].each do |f|
print "#{f} is a: "
if File.directory?(f)
puts "directory"
else
puts "file"
end
end

Please note that Dir::[] is an alias to Dir::glob and it will not show
you files starting with a dot unless you use Dir['{,.}*']

^ manveru