Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read add to file

Status
Not open for further replies.

djack2

Programmer
Dec 12, 2000
16
0
0
US
Is there a way to read a file, write new info to the beginning and end of the file and then save it? I know how to append to the file, but I need to add to the top of the file also. Any suggestions?
 
My best bet would be to first READ the file, capture the contents in a variable (for example MyContent). Then manipulate the variable MyContent, and write the manipulated file to the filesystem. Sounds complicated, but check out the following code:


<cfset TheFile=&quot;C:\inetpub\<cfset TextToAdd=&quot;Hello World!&quot;>
<cfset BR=&quot;#Chr(13)#&quot;&amp;&quot;#Chr(10)#&quot;>

<cffile action=&quot;READ&quot;
file=&quot;#TheFile#&quot;
variable=&quot;MyContent&quot;>

<cfset MyNewContent=&quot;#TextToAdd#&quot;&amp;&quot;#BR#&quot;&amp;&quot;#MyContent#&quot;>

<cffile action=&quot;WRITE&quot;
file=&quot;#TheFile#&quot;
output=&quot;#MyNewContent#&quot;
addnewline=&quot;Yes&quot;>

Beautiful isn't it? ;-)
<webguru>iqof188</webguru>
 
Ok, thanks. I'll give that a try.
 
That code worked great! You just saved me about 2 days of work. I had to manually edit a few thousand files and this
just helped me automate the process. I used a loop and cfdirectory to automatically open and edit each file in each directory and insert additional code. Once again, thanks for your help iqof188.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top