Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > append static text for all files in a directory

Reply
Thread Tools

append static text for all files in a directory

 
 
Srinath A.
Guest
Posts: n/a
 
      02-24-2009
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 !!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Jun Young Kim
Guest
Posts: n/a
 
      02-24-2009
If your code is normally executed, the following code will give you =20
some hint.

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

2009. 02. 24, =BF=C0=C8=C4 3:16, Srinath A. =C0=DB=BC=BA:

> 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 =3D File.open("application.rb","a") do |f|
> f.puts "append to file";
> f.close();
> end
>
>
>
> thanks !!
> --=20
> Posted via http://www.ruby-forum.com/.
>
>






 
Reply With Quote
 
 
 
 
Srinath A.
Guest
Posts: n/a
 
      02-24-2009
Hi jun,

thanks for reply.

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

thanks !

Jun Young Kim wrote:
> If your code is normally executed, the following code will give you
> some hint.
>
> Dir.glob("*.rb") do |file|
> puts file
> end
>
> 2009. 02. 24, ���� 3:16, Srinath A. �ۼ�:


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Simon Krahnke
Guest
Posts: n/a
 
      02-24-2009
* Srinath A. <(E-Mail Removed)> (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
 
Reply With Quote
 
Srinath A.
Guest
Posts: n/a
 
      02-25-2009
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 Krahnke wrote:
> * Srinath A. <(E-Mail Removed)> (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


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.24 All I want to do is append a small amount of text to the end of a file. Do I still have to use locking? PerlFAQ Server Perl Misc 0 03-31-2011 04:00 AM
FAQ 5.24 All I want to do is append a small amount of text to the end of a file. Do I still have to use locking? PerlFAQ Server Perl Misc 0 02-01-2011 05:00 PM
the address of list.append and list.append.__doc__ HYRY Python 10 09-26-2007 09:41 AM
"Insert" (not append) new text segment to an existing text file sm C++ 2 04-29-2005 03:55 PM
list all css files in a directory and sub directory TJS ASP .Net 1 06-23-2004 10:49 AM



Advertisments