From: lasitha Date: 2009-03-17T03:05:37+09:00 Subject: Re: handling many files inside dir On Mon, Mar 16, 2009 at 7:52 PM, Bary Buz wrote: > [ ... ] > 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 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 = 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 = File.open(filename) do |file| Manipulate.new(file) end Hope some of that is helpful. lasitha