From: Scott Barron Date: 2006-10-22T08:14:39+09:00 Subject: Re: What is wrong with my code ------=_Part_129158_24327858.1161472476477 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 10/21/06, Li Chen wrote: > > Hi folks, > > I write two srcipts to check files in a given directory. Version 1 uses > regular expression and works but I cann't find the size for each file. > Version 2 uses Ruby built-in File.file? method but it doesn't work at > all. I wonder what is going on with my scripts? > > Thank you, > > Li > > #######Version 1: > > path='c:\path\to\folder' > Dir.open(path).each do |file| > if file=~/(\w+|d+).(\d{3,})/ > puts file > puts File.size(file)# this line doesn't work > file_number+=1 > end > end > > puts > puts 'Files',path > puts 'File number',file_number > > #######Version2 > path='c:\path\to\folder' > Dir.open(path).each do |file| > if File.file?(file) > puts file > puts File.size(file) > file_number+=1 > end > end > > puts > puts 'Files',path > puts 'File number',file_number > > -- > Posted via http://www.ruby-forum.com/. file in those cases should just be the file name, without the path info. So unless the directory you've opened is your current directory, File.sizewon't work right. Try File.size(File.join(path, file)) (same goes for File.file?) -Scott ------=_Part_129158_24327858.1161472476477--