about file format

Discussion in 'Ruby' started by Li Chen, Oct 22, 2006.

  1. Li Chen

    Li Chen Guest

    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

    --
    Posted via http://www.ruby-forum.com/.
     
    Li Chen, Oct 22, 2006
    #1
    1. Advertising

  2. Li Chen

    EB Guest

    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, Oct 22, 2006
    #2
    1. Advertising

  3. Li Chen

    Li Chen Guest

    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

    --
    Posted via http://www.ruby-forum.com/.
     
    Li Chen, Oct 22, 2006
    #3
  4. Li Chen

    Li Chen Guest

    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

    --
    Posted via http://www.ruby-forum.com/.
     
    Li Chen, Oct 22, 2006
    #4
  5. Li Chen

    Kalman Noel Guest

    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
     
    Kalman Noel, Oct 22, 2006
    #5
  6. Li Chen

    EB Guest

    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
     
    EB, Oct 22, 2006
    #6
  7. Li Chen

    Li Chen Guest


    > 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










    --
    Posted via http://www.ruby-forum.com/.
     
    Li Chen, Oct 22, 2006
    #7
  8. Li Chen

    Li Chen Guest

    Scott Barron 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


    --
    Posted via http://www.ruby-forum.com/.
     
    Li Chen, Oct 22, 2006
    #8
  9. Li Chen

    EB Guest

    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
     
    EB, Oct 24, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    719
    Magnus Lycka
    Oct 27, 2006
  2. Replies:
    1
    Views:
    806
    mlimber
    Sep 11, 2006
  3. Randy Kramer
    Replies:
    2
    Views:
    396
    Randy Kramer
    Jan 12, 2007
  4. Wesley
    Replies:
    1
    Views:
    137
    Wesley
    Apr 15, 2014
Loading...

Share This Page