Working with file types and directories

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

···

--
John Maclean
MSc (DIC)
07739 171 531

John Maclean wrote:

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

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 }

···

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

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 John Maclean <info@jayeola.org> wrote:

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

--
John Maclean
MSc (DIC)
07739 171 531

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

···

<vjoel@path.berkeley.edu> wrote:

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

--
John Maclean
MSc (DIC)
07739 171 531

John Maclean wrote:

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

#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) }

···

--
        vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

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.

···

--
kjana@dm4lab.to January 29, 2006
Youth comes but once in a lifetime.

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.

···

On
Sat, 28 Jan 2006 14:56:21 +0900 Joel VanderWerf
<vjoel@path.berkeley.edu> wrote:

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

Thanks for that reply...

I've gone through the great guide here Learn to Program, by Chris Pine 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 YANAGAWA Kazuhisa <kjana@dm4lab.to> wrote:

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.

--
John Maclean
MSc (DIC)
07739 171 531

Yesss!!! Thanks :slight_smile:
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 Alexis Reigel <mail@koffeinfrei.org> wrote:

> 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.

> On
> Sat, 28 Jan 2006 14:56:21 +0900 Joel VanderWerf
> <vjoel@path.berkeley.edu> wrote:
>
>
>>Dir.foreach(".") {|x| puts File.ftype x }
>
>

--
John Maclean
MSc (DIC)
07739 171 531