# About file format

**URL:** <https://www.ruby-forum.com/t/about-file-format/73196>\
**Category:** Ruby\
**Created:** [October 22, 2006, 6:52pm UTC](https://www.ruby-forum.com/t/about-file-format/73196 "2006-10-22T18:52:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 6:52pm UTC](https://www.ruby-forum.com/t/about-file-format/73196/1 "2006-10-22T18:52:17Z")

</div>

Hi guys,

I just wonder what is the default file format which can recognized by  
File.file?(). I have some files in a folder with this format:

OTI-4.001  
OTI-4.002  
OTI-4.003  
OTI-4.004  
OTI-4.005  
OTI-4.006  
OTI-4.007

I use irb to check if this format can be seen by Ruby and here is the  
result:

irb(main):007:0\> File.file?(‘OTI-4.001’)  
=\> false

Any comments?

Thank you in advance,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/about-file-format/73196/2 "2006-10-22T19:12:11Z")

</div>

Li Chen wrote:

> Hi guys,
> 
> I just wonder what is the default file format which can recognized by  
> File.file?(). I have some files in a folder with this format:
> 
> OTI-4.001  
> OTI-4.002  
> \<…\>
> 
> I use irb to check if this format can be seen by Ruby and here is the  
> result:
> 
> irb(main):007:0\> File.file?(‘OTI-4.001’)  
> =\> false

Li,

Try again, I just did the following on my Linux box and  
it worked fine:

$ touch OTI-4.001

$ ls OTI-4.001  
OTI-4.001

$ irb  
irb(main):001:0\> File.file?(‘OTI-4.001’)  
=\> true  
irb(main):002:0\>

EB

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 7:25pm UTC](https://www.ruby-forum.com/t/about-file-format/73196/3 "2006-10-22T19:25:36Z")

</div>

EB wrote:

> Try again, I just did the following on my Linux box and  
> it worked fine:
> 
> $ touch OTI-4.001
> 
> $ ls OTI-4.001  
> OTI-4.001
> 
> $ irb  
> irb(main):001:0\> File.file?(‘OTI-4.001’)  
> =\> true  
> irb(main):002:0\>
> 
> EB

Unfortunately I work under XP.

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 7:37pm UTC](https://www.ruby-forum.com/t/about-file-format/73196/4 "2006-10-22T19:37:12Z")

</div>

EB wrote:

> Try again, I just did the following on my Linux box and  
> it worked fine:
> 
> $ touch OTI-4.001
> 
> $ ls OTI-4.001  
> OTI-4.001
> 
> $ irb  
> irb(main):001:0\> File.file?(‘OTI-4.001’)  
> =\> true  
> irb(main):002:0\>
> 
> EB

Hi,

I try different formats and these are what I get:

C:\Ruby\self\>irb  
irb(main):001:0\> File:file?(ruby.rb)  
SyntaxError: compile error  
(irb):1: parse error, unexpected ‘(’, expecting $  
File:file?(ruby.rb)  
^  
from (irb):1  
irb(main):002:0\> File.file?(“ruby.rb”)  
=\> true  
irb(main):003:0\> File.file?(‘ruby.rb’)  
=\> true  
irb(main):004:0\> File.file?(“ruby1.rb”)  
=\> false  
irb(main):005:0\> File.file?(‘ruby1.rb’)  
=\> false  
irb(main):006:0\> File.file?(“ruby-1.rb”)  
=\> false  
irb(main):007:0\> File.file?(‘ruby-1.rb’)  
=\> false  
irb(main):008:0\>

It seems Ruby only see ruby.rb as a file but not ruby-1.rb or ruby1.rb  
as a file under XP

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/about-file-format/73196/5 "2006-10-22T19:46:23Z")

</div>

Li Chen wrote:

> Unfortunately I work under XP.

Shouldn’t make a difference, that’s one of the nice things  
about Ruby, its portability.

However, just to be sure, I redid it under XP:

C:\Ruby\>dir OTI\*  
Volume in drive C has no label.  
Volume Serial Number is 941C-BE4A

Directory of C:\Ruby

10/15/2006 18:21 34 OTI-4.001  
1 File(s) 34 bytes  
0 Dir(s) 19,644,063,744 bytes free

C:\bonak\Ruby\>irb  
irb(main):001:0\> File.file?(‘OTI-4.001’)  
=\> true  
irb(main):002:0\>

Works here too … so I am not sure why yours is not  
working. Did you try again? I have managed to get irb  
confused before and once I restarted things worked well.

eb

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 9:20pm UTC](https://www.ruby-forum.com/t/about-file-format/73196/6 "2006-10-22T21:20:57Z")

</div>

Li Chen wrote:

/ …

> irb(main):001:0\> File:file?(ruby.rb)  
> SyntaxError: compile error  
> (irb):1: parse error, unexpected ‘(’, expecting $  
> File:file?(ruby.rb)  
> ^  
> from (irb):1

You must put quotation marks around the file name.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/about-file-format/73196/7 "2006-10-22T19:46:22Z")

</div>

Li Chen:

> irb(main):003:0\> File.file?(‘ruby.rb’)  
> =\> true  
> irb(main):004:0\> File.file?(“ruby1.rb”)  
> =\> false

What does Dir.entries(’.’) say in the same directory?

Kalman

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/about-file-format/73196/8 "2006-10-22T22:47:19Z")

</div>

On 10/22/06, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > =\> true  
> > I figure out why it doesn’t work: irb only looks for the file name in  
> > if file\_name=~/(\w+|d+).(\d{3,})/  
> > C:\Ruby\self\>dir5.rb  
> > ####script2–Doesn’t work  
> > end  
> > Files  
> > I:\Common\Gao\Notebooks\Flow\OT1\OTI-4  
> > File number  
> > 0
> 
> But right now I just don’t know how to make script 2 work.
> 
> Li

As was said in another thread, you need to assemble the full path to the  
file.

Dir.foreach(path) do |file\_name|  
if File.file?(File.join(path, file\_name))  
# …  
end  
end

-Scott

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/about-file-format/73196/9 "2006-10-22T21:48:45Z")

</div>

> Directory of C:\Ruby
> 
> 10/15/2006 18:21 34 OTI-4.001  
> 1 File(s) 34 bytes  
> 0 Dir(s) 19,644,063,744 bytes free
> 
> C:\bonak\Ruby\>irb  
> irb(main):001:0\> File.file?(‘OTI-4.001’)  
> =\> true  
> irb(main):002:0\>
> 
> Works here too … so I am not sure why yours is not  
> working. Did you try again? I have managed to get irb  
> confused before and once I restarted things worked well.
> 
> eb

Hi EB,

I figure out why it doesn’t work: irb only looks for the file name in  
current directory. I try on both linux and xp.  
But the files I want to search are in a different drive under XP.  
Actually this question comes from the following script1 and script2:

########### script1–It works perfect.  
file\_number=0  
path=“I:\Common\Gao\Notebooks\Flow\OT1\OTI-4”

Dir.foreach(path) do |file\_name|

```
  if file_name=~/(\w+|d+).(\d{3,})/
      puts file_name
      file_number+=1
  end

```

end

puts  
puts ‘Files’,path  
puts ‘File number’,file\_number

############Screen ouptut  
C:\Ruby\self\>dir5.rb  
OTI-4.001  
…  
OTI-4.025  
OTI-4.026

Files  
I:\Common\Gao\Notebooks\Flow\OT1\OTI-4  
File number  
26

####script2–Doesn’t work

file\_number=0  
path=“I:\Common\Gao\Notebooks\Flow\OT1\OTI-4”

Dir.foreach(path) do |file\_name|

```
    if File.file?(file_name)
         puts file_name
         puts File.size(file_name)
         file_number+=1
    end

```

end

puts  
puts ‘Files’,path  
puts ‘File number’,file\_number

##screen output

C:\Ruby\self\>dir5.rb

Files  
I:\Common\Gao\Notebooks\Flow\OT1\OTI-4  
File number  
0

But right now I just don’t know how to make script 2 work.

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 11:29pm UTC](https://www.ruby-forum.com/t/about-file-format/73196/10 "2006-10-22T23:29:52Z")

</div>

Scott B. wrote:

> As was said in another thread, you need to assemble the full path to the  
> file.
> 
> Dir.foreach(path) do |file\_name|  
> if File.file?(File.join(path, file\_name))  
> # …  
> end  
> end

Hi,

Robert also privodes another solution one:

##### 

require ‘find’

path=‘I:/Common/xxx/Notebooks/Flow/OT1/OTI-4’

file\_number = 0

Find.find(path) do |f|

puts f if File.file?(f) && f=~/.\d+$/  
# print out the file with format xxx.001 xxx.002…  
file\_\_number+=1

end

puts file\_number

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 24, 2006, 1:30pm UTC](https://www.ruby-forum.com/t/about-file-format/73196/11 "2006-10-24T13:30:54Z")

</div>

Li Chen wrote:

> I figure out why it doesn’t work: irb only looks for the file name in  
> current directory.

Yup, that’s why I showed a directory listing each time  
before I ran my examples 🙂

EB
