# Working with file types and directories

**URL:** <https://www.ruby-forum.com/t/working-with-file-types-and-directories/98180>\
**Category:** Ruby\
**Created:** [January 28, 2006, 5:17am UTC](https://www.ruby-forum.com/t/working-with-file-types-and-directories/98180 "2006-01-28T05:17:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 28, 2006, 5:17am UTC](https://www.ruby-forum.com/t/working-with-file-types-and-directories/98180/1 "2006-01-28T05:17:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 28, 2006, 6:56am UTC](https://www.ruby-forum.com/t/working-with-file-types-and-directories/98180/2 "2006-01-28T06:56:46Z")

</div>

John M. 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 }

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 28, 2006, 1:16pm UTC](https://www.ruby-forum.com/t/working-with-file-types-and-directories/98180/3 "2006-01-28T13:16:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 28, 2006, 1:49pm UTC](https://www.ruby-forum.com/t/working-with-file-types-and-directories/98180/4 "2006-01-28T13:49:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 28, 2006, 2:41pm UTC](https://www.ruby-forum.com/t/working-with-file-types-and-directories/98180/5 "2006-01-28T14:41:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 28, 2006, 7:44pm UTC](https://www.ruby-forum.com/t/working-with-file-types-and-directories/98180/6 "2006-01-28T19:44:59Z")

</div>

John M. 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) }

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 29, 2006, 5:41am UTC](https://www.ruby-forum.com/t/working-with-file-types-and-directories/98180/7 "2006-01-29T05:41:27Z")

</div>

In Message-Id: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 28, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/working-with-file-types-and-directories/98180/8 "2006-01-28T15:35:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 29, 2006, 12:40pm UTC](https://www.ruby-forum.com/t/working-with-file-types-and-directories/98180/9 "2006-01-29T12:40:27Z")

</div>

Thanks for that reply…

I’ve gone through the great guide here [Learn to Program, by Chris Pine](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
