handling many files inside dir

B

Bary Buz

Hello,

im trying to process a directory that contains many text files. I want
to read each file, store the text inside file and then move to the next
file. I used the following code but at the end i get an error about
Permission Denied. Can someone tell me what im doing wrong.

#get list of files

basedir = "C:/rubyfolder"
Dir.chdir(basedir)
Dir.open(basedir) do |dir|
dir.each do |file|
x = file
myfile = File.open(x, "r")
x.close
dataclass = Manipulate.new(myfile)
end
end

thanks in advance
 
L

lasitha

[ ... ]
file. I used the following code but at the end i get an error about
Permission Denied. Can someone tell me what im doing wrong.

#get list of files

basedir =3D "C:/rubyfolder"
Dir.chdir(basedir)
Dir.open(basedir) do |dir|
=A0dir.each do |file|
=A0x =3D file
=A0myfile =3D File.open(x, "r")
=A0x.close
=A0dataclass =3D Manipulate.new(myfile)
=A0end
end
Hello Barry,

Your error probably has nothing to do with ruby. Check whether you
(the user this ruby process is running as) has read access to
C:/rubyfolder and the files within. The first couple lines of the
error stacktrace should help you track down what you don't have
permissions on.

In the meantime, some other observations:

1: --------
Dir.open(basedir) do |dir|
dir.each do |file|
...

is equivalent to:

Dir.foreach(basedir) do |file|
...

2: --------
dir.each do |file|
x =3D file

This is redundant. All you're doing is creating another reference
('x') to the same object referenced by 'file'. If you prefer the name
'x', you might as well write 'dir.each do |x|'. But more likely
you'll just want to use the 'file' block variable directly (or perhaps
rename it to 'filename').

3: --------
x.close

This won't work. Dir#each passes each filename into the block as a
String. So 'x' won't have a #close method. On a related note, a nice
way to ensure a file is closed after you're done with it is to use the
block form of File#open:

dataclass =3D File.open(filename) do |file|
Manipulate.new(file)
end


Hope some of that is helpful.
lasitha
 
R

Robert Klemme

im trying to process a directory that contains many text files. I want
to read each file, store the text inside file and then move to the next
file. I used the following code but at the end i get an error about
Permission Denied. Can someone tell me what im doing wrong.

Where do you get that error? Can you provide the stacktrace and
indicate where that is in the script?
#get list of files

basedir = "C:/rubyfolder"
Dir.chdir(basedir)
Dir.open(basedir) do |dir|

This won't work as soon as basedir is a relative path!
dir.each do |file|
x = file
myfile = File.open(x, "r")
x.close
dataclass = Manipulate.new(myfile)
end
end

thanks in advance

See also lasitha's excellent remarks.

Cheers

robert
 

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

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top