Forum: Ruby How can i determine the file type?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sasaki (Guest)
on 2007-02-24 09:57
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
Chris S. (Guest)
on 2007-02-24 10:35
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
Chris S. (Guest)
on 2007-02-24 10:35
(Received via mailing list)
On Feb 24, 12:57 am, Sasaki <removed_email_address@domain.invalid> wrote:
>     Find.find(dir)do |path|
> 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
Austin Z. (Guest)
on 2007-02-24 17:07
(Received via mailing list)
On 2/24/07, Sasaki <removed_email_address@domain.invalid> 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 U.' Society) to pure Ruby.

-austin
Chad P. (Guest)
on 2007-02-24 21:27
(Received via mailing list)
On Sat, Feb 24, 2007 at 05:35:42PM +0900, Chris S. 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.
This topic is locked and can not be replied to.