What is wrong with my code

L

Li Chen

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
 
C

camenix

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

Use File.extend_path to convert path to absolute path.I think there is
something wrong with the file.
 
R

Robert Klemme

Li said:
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?

So you want to sum sizes of all files in a directory hierarchy whose
names match a certain pattern. The pattern you use cannot be used with
Dir[] for filtering. So you better use find:

sum = 0
Find.find(path) do |f|
sum += File.size(f) if
/(\w+|d+).(\d{3,})/ =~ File.basename(f) && File.file?(f)
end

Note, your regexp might not match what you actually think it matches.
At the moment you match all file names that contain (!) at least a
single digit or word character followed by any character and then at
least three digits. I am guessing here but do you maybe rather want all
files that have a purely numeric file extension? In that case this
regexp would be better

/\.\d+$/

Regards

robert
 
L

Li Chen

Robert Klemme wrote:
...
So you want to sum sizes of all files in a directory hierarchy whose
names match a certain pattern. The pattern you use cannot be used with
Dir[] for filtering. So you better use find:

sum = 0
Find.find(path) do |f|
sum += File.size(f) if
/(\w+|d+).(\d{3,})/ =~ File.basename(f) && File.file?(f)
end
...

Hi Robert,

Thank you very much for the code. Based on what I understand and what I
need I make some changes. But I still have some questions:1) How do I
factor the print or format codes here? 2) The outputs of the file are
in reverse order how do I print them out in this format; xxx.001,
xxx.002,...,xxx.026 3) what is the purpose of File.basename here?

Thanks,

Li


#dir6.rb find the file number in a folder
require 'find'

path='I:/Common/Gao/Notebooks/Flow/OT1/OTI-4'


file_number = 0
Find.find(path) do |f|
#sum += File.size(f) if
# /(\w+|d+).(\d{3,})/ =~ File.basename(f) && File.file?(f)
#puts f if /(\w+|d+).(\d{3,})/ =~ File.basename(f) && File.file?(f)
if File.file?(f) && f=~/(\w+|d+).(\d{3,})/
print f,"\t"
printf("%10s %10s", File.size(f),'byte' )
puts
file_number+=1
end
end

puts file_number

###screen output


I:/Common/Gao/Notebooks/Flow/OT1/OT1-typing-3/OT1-3.028 4796348
byte
....
I:/Common/Gao/Notebooks/Flow/OT1/OT1-typing-3/OT1-3.001 11877548
byte
28
 

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,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top