# File name and content not corresponding

**URL:** <https://www.ruby-forum.com/t/file-name-and-content-not-corresponding/107056>\
**Category:** Ruby\
**Created:** [June 17, 2007, 12:16pm UTC](https://www.ruby-forum.com/t/file-name-and-content-not-corresponding/107056 "2007-06-17T12:16:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [June 17, 2007, 12:16pm UTC](https://www.ruby-forum.com/t/file-name-and-content-not-corresponding/107056/1 "2007-06-17T12:16:54Z")

</div>

Hi folks,

## The code below almost works perfect. Thus it creates the text files with the right content. However, the name of the text file should correspond to the Bugtraq ID. As of now it’s Bugtraq ID 1 and 3.txt, 2-2, and Bugtraq ID 3

1.txt. What do I have to correct? Is it index - 1?

Thanks,  
Tom

# Initialize Logger

require ‘logger’  
root\_logger = Logger.new ‘root.log’  
root\_logger.level = Logger::INFO

# Setup at\_exit hook

at\_exit { root\_logger.info ‘done.’ }

%w{ rubygems hpricot open-uri }.each { |lib| require lib }  
URL\_MASK =  
‘[http://www.securityfocus.com/bid/%i](http://www.securityfocus.com/bid/%25i)’.freeze[http://www.securityfocus.com/bid/%i’.freeze](http://www.securityfocus.com/bid/%25i%27.freeze)

tgroup = ThreadGroup.new

root\_logger.info ‘Parsing SecurityFocus…’  
( 1 … 3 ).each do |id|  
root\_logger.info " open #{ URL\_MASK % id }"  
thread = Thread.new do  
begin  
Hpricot open( URL\_MASK % id )  
rescue =\> error  
root\_logger.error error.inspect  
end  
end  
tgroup.add thread  
end

tgroup.list.each\_with\_index do |thread, index|  
if doc = thread.value  
File.open( “#{ index + 1 }.txt”, ‘w+’ ) do |file|  
file \<\< ( doc/‘#vulnerability’ ).inner\_text  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [June 18, 2007, 11:59am UTC](https://www.ruby-forum.com/t/file-name-and-content-not-corresponding/107056/2 "2007-06-18T11:59:33Z")

</div>

On 6/17/07, Tom B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi folks,
> 
> The code below almost works perfect. Thus it creates the text files with the  
> right content. However, the name of the text file should correspond to the  
> Bugtraq ID. As of now it’s Bugtraq ID 1 and 3.txt, 2-2, and Bugtraq ID 3 -  
> 1.txt. What do I have to correct? Is it index - 1?
> 
> Thanks,  
> Tom

What about more explicit id using thread-local storage:  
…

> ```
> end
> 
> ```
> 
> end

- thread[:bug\_id] = id

> tgroup.add thread  
> end

- tgroup.list.each\_with\_index do |thread, index|

- tgroup.list.each do |thread|

> if doc = thread.value

- 

```
File.open( "#{ index + 1 }.txt", 'w+' ) do |file|

```

- 

```
File.open( "#{thread[:bug_id]}.txt", 'w+' ) do |file|

```
