How can i determine the file type?

S

Sasaki

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
 
C

Chris Shea

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 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
 
C

Chris Shea

Sasaki said:
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
 
A

Austin Ziegler

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
 
C

Chad Perrin

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top