How can i determine the file type?

Discussion in 'Ruby' started by Sasaki, Feb 24, 2007.

  1. Sasaki

    Sasaki Guest

    hi guys,
    How can i determine the file type?
    I have to do these actions:

    1. I have tor read all files from a directory and check them
    2.after checking i have to read only the excel files.

    I have tried with the code..
    initial_dir = ["./upload_dir"]
    for dir in initial_dir
    Find.find(dir)do |path|
    files_list << path
    file_part= path.split('/')
    file_type=file_part[1].split('.')
    if(file_type[1]=='xls')
    read the file

    end
    end
    end

    error is "Exception: Permission denied - ./upload_dir"
    probably, the code also try to process the "../" and "./"

    So how can i check the file type.

    please help me waiting for ur response

    Sasaki

    --
    Posted via http://www.ruby-forum.com/.
    Sasaki, Feb 24, 2007
    #1
    1. Advertising

  2. Sasaki

    Chris Shea Guest

    On Feb 24, 12:57 am, Sasaki <> wrote:
    > hi guys,
    > How can i determine the file type?
    > I have to do these actions:
    >
    > 1. I have tor read all files from a directory and check them
    > 2.after checking i have to read only the excel files.
    >
    > I have tried with the code..
    > initial_dir = ["./upload_dir"]
    > for dir in initial_dir
    > Find.find(dir)do |path|
    > files_list << path
    > file_part= path.split('/')
    > file_type=file_part[1].split('.')
    > if(file_type[1]=='xls')
    > read the file
    >
    > end
    > end
    > end
    >
    > error is "Exception: Permission denied - ./upload_dir"
    > probably, the code also try to process the "../" and "./"
    >
    > So how can i check the file type.
    >
    > please help me waiting for ur response
    >
    > Sasaki
    >
    > --
    > Posted viahttp://www.ruby-forum.com/.


    If you're trusting the file extension (which I see no real problem
    with) and you're only dealing with files in the dir (and not any
    subdirs), you could do something like this:

    initial_dir = './upload_dir'
    Dir.entries(initial_dir).each do |file|
    if file.split('.').last == 'xls'
    #process file
    end
    end

    --
    Chris
    Chris Shea, Feb 24, 2007
    #2
    1. Advertising

  3. Sasaki

    Chris Shea Guest

    Sasaki wrote:
    > hi guys,
    > How can i determine the file type?
    > I have to do these actions:
    >
    > 1. I have tor read all files from a directory and check them
    > 2.after checking i have to read only the excel files.
    >
    > I have tried with the code..
    > initial_dir = ["./upload_dir"]
    > for dir in initial_dir
    > Find.find(dir)do |path|
    > files_list << path
    > file_part= path.split('/')
    > file_type=file_part[1].split('.')
    > if(file_type[1]=='xls')
    > read the file
    >
    > end
    > end
    > end
    >
    > error is "Exception: Permission denied - ./upload_dir"
    > probably, the code also try to process the "../" and "./"
    >
    > So how can i check the file type.
    >
    > please help me waiting for ur response
    >
    > Sasaki


    If you're trusting the file extension (which I see no problem with), and
    you're only dealing with files in the directory specified (no subdirs)
    you could do something like this:

    Dir.entries('./upload_dir').each do |file|
    if file.split('.').last == 'xls'
    # process file
    end
    end

    HTH,
    Chris

    --
    Posted via http://www.ruby-forum.com/.
    Chris Shea, Feb 24, 2007
    #3
  4. On 2/24/07, Sasaki <> wrote:
    > hi guys,
    > How can i determine the file type?
    > I have to do these actions:
    >
    > 1. I have tor read all files from a directory and check them
    > 2.after checking i have to read only the excel files.


    Look at MIME::Types -- it will help. Others are also working on a port
    of libmagic (the Calgary Ruby Users' Society) to pure Ruby.

    -austin
    --
    Austin Ziegler * * http://www.halostatue.ca/
    * * http://www.halostatue.ca/feed/
    *
    Austin Ziegler, Feb 24, 2007
    #4
  5. Sasaki

    Chad Perrin Guest

    On Sat, Feb 24, 2007 at 05:35:42PM +0900, Chris Shea wrote:
    >
    > If you're trusting the file extension (which I see no problem with), and


    That probably wouldn't be my preferred approach. Trusting file
    extensions is only a mostly-solution, and only on platforms where file
    extensions are used by applications to determine whether they should try
    to open the files. Relying on the file extension to determine filetype
    leads to issues with both portability and security, in my experience.

    On the other hand, if this is only for personal use and meant to be
    something of a throw-away script, I'm sure that doesn't matter much.

    --
    CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    "The ability to quote is a serviceable
    substitute for wit." - W. Somerset Maugham
    Chad Perrin, Feb 24, 2007
    #5
    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. Spotted Owl Eater

    Determine the File Type of an Uploaded File

    Spotted Owl Eater, Nov 16, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,839
    Patrice
    Nov 16, 2005
  2. Gernot Frisch

    how to determine type-type?

    Gernot Frisch, Jan 12, 2005, in forum: C++
    Replies:
    3
    Views:
    389
    Ulrich Achleitner
    Jan 13, 2005
  3. Sami Viitanen

    Determine file type (binary or text)

    Sami Viitanen, Aug 13, 2003, in forum: Python
    Replies:
    21
    Views:
    14,936
  4. Peña, Botp
    Replies:
    1
    Views:
    217
    Robert Klemme
    Jan 24, 2004
  5. Nick Weavers
    Replies:
    4
    Views:
    119
Loading...

Share This Page