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

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

On Feb 24, 12:57 am, Sasaki [email protected] 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

On 2/24/07, Sasaki [email protected] 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

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.