How can i determine the file type?

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/.

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

···

On Feb 24, 12:57 am, Sasaki <pb_sas...@e-sao.co.jp> 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/.

--
Chris

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/\.

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

···

On 2/24/07, Sasaki <pb_sasaki@e-sao.co.jp> 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.

--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca

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.

···

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

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"The ability to quote is a serviceable
substitute for wit." - W. Somerset Maugham