# Append static text for all files in a directory

**URL:** <https://www.ruby-forum.com/t/append-static-text-for-all-files-in-a-directory/161182>\
**Category:** Ruby\
**Created:** [February 24, 2009, 7:17am UTC](https://www.ruby-forum.com/t/append-static-text-for-all-files-in-a-directory/161182 "2009-02-24T07:17:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![srinathbtech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@srinathbtech](https://www.ruby-forum.com/u/srinathbtech)\
**Post date:** [February 24, 2009, 7:17am UTC](https://www.ruby-forum.com/t/append-static-text-for-all-files-in-a-directory/161182/1 "2009-02-24T07:17:21Z")

</div>

How to append static text for all files in application .  
I want to append comment lines on top of each file in app/controllers/

how can iterate and append in the app ?

I used this for single file :

filename = File.open(“application.rb”,“a”) do |f|  
f.puts “append to file”;  
f.close();  
end

thanks !!

---

<div class="post-metadata">

**Author:** ![srinathbtech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@srinathbtech](https://www.ruby-forum.com/u/srinathbtech)\
**Post date:** [February 24, 2009, 7:45am UTC](https://www.ruby-forum.com/t/append-static-text-for-all-files-in-a-directory/161182/2 "2009-02-24T07:45:39Z")

</div>

If your code is normally executed, the following code will give you  
some hint.

Dir.glob("\*.rb") do |file|  
puts file  
end

1. 
  1. 24, ¿ÀÈÄ 3:16, Srinath A. ÀÛ¼º:

---

<div class="post-metadata">

**Author:** ![srinathbtech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@srinathbtech](https://www.ruby-forum.com/u/srinathbtech)\
**Post date:** [February 24, 2009, 7:51am UTC](https://www.ruby-forum.com/t/append-static-text-for-all-files-in-a-directory/161182/3 "2009-02-24T07:51:28Z")

</div>

Hi jun,

thanks for reply.

how can we append text on top of each file in all .rb files through  
iterating ??

thanks !

Jun Y. Kim wrote:

> If your code is normally executed, the following code will give you  
> some hint.
> 
> Dir.glob("\*.rb") do |file|  
> puts file  
> end
> 
> 1. 
> 1. 24, ï¿½ï¿½ï¿½ï¿½ 3:16, Srinath A. ï¿½Û¼ï¿½:

---

<div class="post-metadata">

**Author:** ![srinathbtech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@srinathbtech](https://www.ruby-forum.com/u/srinathbtech)\
**Post date:** [February 24, 2009, 9:07am UTC](https://www.ruby-forum.com/t/append-static-text-for-all-files-in-a-directory/161182/4 "2009-02-24T09:07:47Z")

</div>

- Srinath A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (07:16) schrieb:

> How to append static text for all files in application .  
> I want to append comment lines on top of each file in app/controllers/

Appending is “inserting” text at the end of the file. If you want to  
modify something before the end you got to read it all in, do your  
changes, and write it out again.

If your Files are small enough to be read in whole, it’s quite easy in  
Ruby:

content = File.read(filename)  
File.open(filename, “w”) do | f |  
f \<\< “# comment\n”  
f \<\< content  
end

mfg, simon … untested

---

<div class="post-metadata">

**Author:** ![srinathbtech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@srinathbtech](https://www.ruby-forum.com/u/srinathbtech)\
**Post date:** [February 25, 2009, 6:36am UTC](https://www.ruby-forum.com/t/append-static-text-for-all-files-in-a-directory/161182/5 "2009-02-25T06:36:43Z")

</div>

The code below was working fine , but deleting the first line in the  
files and writing the added one :

Dir.glob(“application.rb”) do |file|  
File.open(file,“r+”) do |f|  
puts file  
f.puts “Commenting is very common style.\nCommenting helps more easy  
readability”  
end  
end

how can we prepend text in file with out deleting the existing content  
??

thanks !!

Simon K. wrote:

> - Srinath A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (07:16) schrieb:
> 
> > How to append static text for all files in application .  
> > I want to append comment lines on top of each file in app/controllers/
> 
> Appending is “inserting” text at the end of the file. If you want to  
> modify something before the end you got to read it all in, do your  
> changes, and write it out again.
> 
> If your Files are small enough to be read in whole, it’s quite easy in  
> Ruby:
> 
> content = File.read(filename)  
> File.open(filename, “w”) do | f |  
> f \<\< “# comment\n”  
> f \<\< content  
> end
> 
> mfg, simon … untested
