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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

searching a plain text file

Status
Not open for further replies.

ryan

Programmer
Nov 13, 2000
73
US
I'm trying to find out how I can take, for example, an ini file and do the following:

Find the position where the ini file might say "[systemfiles]" and then modify values on that position.

So if I have an ini file called inifile.ini and the position of the word "[systemfiles]" is row 33 then I want to add to row 34 the following "some text". So far the filesystemobject doesn't allow this that I know of. you can import a file using the openastextstream but this only loads the first line of the file. How can I get around this?

Thanks,
Ryan
 
if you don't mind using perlscript you can do this:


<%@Language=PerlScript%>
<%
#open the file.
open(FILE, &quot;inifile.ini&quot;);
#put the contents of the file into an array.
@file = <FILE>;
close(FILE);

#search for the line with [systemfiles].

foreach $file (@file)
{
if($file =~ /^[systemfiles]/)
{
#code for that line... i'm adding text to it.
$line .= &quot;extra text added\n&quot;;
}
}
#reopen the file for writing.
open(FILE, &quot;>inifile.ini&quot;);
foreach $file (@file)
{
print FILE $file;
}
close(FILE)
%>
 
thanks for the help in perl script but any help in vb. i'm going to try to recreate your perl script in vb but not sure .. thanks though luciddream.

ryan
 
I think I got it actually. hehe, all i needed was a little help from trusty msdn october library. you can do this with object.READALL which loads the file into your variable and then you can search on it.

I still don't know how to write a line to a file ON A SPECIFIED ROW though.

Ryan
 
> I still don't know how to write a line to a file ON A SPECIFIED ROW though.

I don't know that you can. You may have to write the entire file with the new lines in the places you want them. So you might do something like this:

Open existing file for reading
Open new file for writing
for each line of existing file
existing.read line
if this is where a new line is needed
new.write line of new text
new.write line that was read from existing file
end for

delete existing file
rename new file to previous existing file name

Good luck
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top