# What is wrong with my code

**URL:** <https://www.ruby-forum.com/t/what-is-wrong-with-my-code/73146>\
**Category:** Ruby\
**Created:** [October 22, 2006, 12:31am UTC](https://www.ruby-forum.com/t/what-is-wrong-with-my-code/73146 "2006-10-22T00:31:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 12:31am UTC](https://www.ruby-forum.com/t/what-is-wrong-with-my-code/73146/1 "2006-10-22T00:31:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 1:15am UTC](https://www.ruby-forum.com/t/what-is-wrong-with-my-code/73146/2 "2006-10-22T01:15:14Z")

</div>

On 10/21/06, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Li  
> end  
> 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/](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

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 2:43am UTC](https://www.ruby-forum.com/t/what-is-wrong-with-my-code/73146/3 "2006-10-22T02:43:21Z")

</div>

Dir.each returns the file, without the path information. You’ll need to  
prepend that and using File.join is a good way to do it.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 9:51am UTC](https://www.ruby-forum.com/t/what-is-wrong-with-my-code/73146/4 "2006-10-22T09:51:58Z")

</div>

> ```
> file_number+=1
> end
> 
> ```
> 
> end
> 
> puts  
> puts ‘Files’,path  
> puts ‘File number’,file\_number
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Use File.extend\_path to convert path to absolute path.I think there is  
something wrong with the file.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 3:06pm UTC](https://www.ruby-forum.com/t/what-is-wrong-with-my-code/73146/5 "2006-10-22T15:06:44Z")

</div>

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?

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

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 23, 2006, 1:45am UTC](https://www.ruby-forum.com/t/what-is-wrong-with-my-code/73146/6 "2006-10-23T01:45:44Z")

</div>

Li Chen wrote:

/ …

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

Tell me what output you want to see and I will tell you how to get it.

> 1. The outputs of the file are  
> in reverse order how do I print them out in this format; xxx.001,  
> xxx.002,…,xxx.026

Push the items into an array, sort the array, print the sorted array.

> 1. what is the purpose of File.basename here?

File.basename gives either a filename without its preceding path, or a  
filename without either its preceding path or its suffix, depending on  
which options are exercised.

Because of the nature of your questions, I want to make you aware of a  
way  
to get some answers on your own:

> **[Ruby-Doc.org: Documenting the Ruby Language](https://ruby-doc.org/)**
>
> Fast, searchable Ruby documentation for core and standard libraries. Plus, links to tutorials, guides, books, and related sites.

… and …

[http://www.ruby-doc.org/docs/ProgrammingRuby/](http://www.ruby-doc.org/docs/ProgrammingRuby/)

The second of these, which can also be downloaded, is a treasure trove  
of  
information. It contains answers to a large percentage of your  
questions.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 5:25am UTC](https://www.ruby-forum.com/t/what-is-wrong-with-my-code/73146/7 "2006-10-22T05:25:49Z")

</div>

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.

Explain. What error messages did you see? You need to provide more  
information.

> I wonder what is going on with my scripts?
> 
> Thank you,
> 
> Li
> 
> #######Version 1:
> 
> path=‘c:\path\to\folder’

Don’t use backslashes like this. Use forward slashes to avoid problems  
in  
interpretation. Ruby will know what to do.

> Dir.open(path).each do |file|  
> if file=~/(\w+|d+).(\d{3,})/  
> puts file  
> puts File.size(file)# this line doesn’t work

“Doesn’t work”? Please tell us what error message you saw. Maybe the  
“file”  
was a directory?

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 23, 2006, 2:39am UTC](https://www.ruby-forum.com/t/what-is-wrong-with-my-code/73146/8 "2006-10-23T02:39:01Z")

</div>

Hi Paul,

Thank you very much. I have a Pickaxe but it is at home.

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 22, 2006, 11:16pm UTC](https://www.ruby-forum.com/t/what-is-wrong-with-my-code/73146/9 "2006-10-22T23:16:44Z")

</div>

Robert K. 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
