About file format

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

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

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

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

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

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.

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

On 10/22/06, Li Chen [email protected] 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

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

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

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 :slight_smile:

EB