Forum: Ruby working with file types and directories

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
508861510639b56d61eb30e6a28b01d9?d=identicon&s=25 John Maclean (Guest)
on 2006-01-28 05:17
(Received via mailing list)
This line shows all the files in the current dir.

This one -should- show all of thier filetypes...

 Dir.foreach(".") {|x| File.ftype x }
=> nil
# what gives?

irb(main):023:0> Dir.foreach(".") {|x| File.ftype(x) }
=> nil

 Dir.foreach(".") {|x| File.ftype(#{x})}
# I knew that this wouldn't work, but I tried it anyway
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2006-01-28 06:56
(Received via mailing list)
John Maclean wrote:
>
>  Dir.foreach(".") {|x| File.ftype(#{x})}
> # I knew that this wouldn't work, but I tried it anyway
>

Try this:

Dir.foreach(".") {|x| puts File.ftype x }

Note that Dir.foreach doesn't return the results of the block. If you
want that behavior:

require 'enumerator'
Dir.enum_for(:foreach, ".").map {|x| File.ftype x }
508861510639b56d61eb30e6a28b01d9?d=identicon&s=25 John Maclean (Guest)
on 2006-01-28 13:16
(Received via mailing list)
Thanks for that reply. I guess that the only way to check that files
are non-empty is to open them, read them and cound the lines, huh?

On
Sat, 28 Jan 2006 14:56:21 +0900 Joel VanderWerf
8342950f34a66d9ec7cfe10b33d5494c?d=identicon&s=25 Alexis Reigel (Guest)
on 2006-01-28 13:49
(Received via mailing list)
> Thanks for that reply. I guess that the only way to check that files
> are non-empty is to open them, read them and cound the lines, huh?

--> FileTest.zero?("file")
Returns true if file is of zero length.
508861510639b56d61eb30e6a28b01d9?d=identicon&s=25 John Maclean (Guest)
on 2006-01-28 14:41
(Received via mailing list)
Yesss!!! Thanks :)
Dir.foreach(".") {|x| puts FileTest.size(x) }
 Dir.foreach(".") {|x| puts FileTest.zero?(x) } # checks for empty
files.

also I'm (finally) getting to grips with using ri.


On Sat, 28 Jan 2006 21:47:11 +0900
508861510639b56d61eb30e6a28b01d9?d=identicon&s=25 John Maclean (Guest)
on 2006-01-28 15:35
(Received via mailing list)
OK, next step...
I can now run tests on files within directories. How can I achieve
something like the following?

 puts "files have been checked" if  Dir.foreach(".") {|x| puts
FileTest.writable?(x) } == "true"

#at the moment I get this:-
irb(main):022:0> puts "files have been checked" if  Dir.foreach(".")
{|x| puts FileTest.writable?(x) } == "true" true
true
true
true
true
true

#but *not* the desired string "files have been checked"

On Sat, 28 Jan 2006 13:16:04 +0900
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2006-01-28 19:44
(Received via mailing list)
John Maclean wrote:
> true
> true
> true
> true
> true
>
> #but *not* the desired string "files have been checked"

Comparing the return value with the string "true" won't work because you
are outputting each writeable? result to stdout, and anyway that output
needs to be and-ed to test if all of the files are writable.

You do want the "files have been checked" output if all files are
writable, right?

Here's one way:

puts "files have been checked" if Dir.new(".").all? {|x|
FileTest.writable?(x) }
7b77d11772fc910ef3cc39d8e891fbdb?d=identicon&s=25 YANAGAWA Kazuhisa (Guest)
on 2006-01-29 05:41
(Received via mailing list)
In Message-Id: <20060129143153.13a84c8e@localhost.localdomain>
John Maclean <info@jayeola.org> writes:

>  puts "files have been checked" if  Dir.foreach(".") {|x| puts
> FileTest.writable?(x) } == "true"

Dir.foreach returns nil, nil != "true" so the condition can not be
met.  I think you want to do the following, right?

  Dir.foreach(".") {|x| puts "file have been checked" if
FileTest.writable?(x)}

where print out the message for each x which is a name of writable
file in the current directory.
508861510639b56d61eb30e6a28b01d9?d=identicon&s=25 John Maclean (Guest)
on 2006-01-29 12:40
(Received via mailing list)
Thanks for that reply...

I've gone through the great guide here http://pine.fm/LearnToProgram/ in
order to get me up to speed with itteration.

 Essentialy what I'd like to do is to check some data within a
directory. Those files are essential to the rest of the program as this
is where the data "lives".

The following line check all files within the dir and prints a message
only if all of the files are writable. It's rather useful to see this
message once as there are loads of files within that dir.

puts "all files have been checked and are writable" if
Dir.new(".").all? {|x| FileTest.writable?(x) }
# this message is only printed once.

Your one-liner prints that message for every time it detects a
writable file.

On Sun, 29 Jan 2006 13:41:22 +0900
This topic is locked and can not be replied to.