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

VBS to change a certain line number in a text file

Status
Not open for further replies.

mark01

Technical User
Jan 17, 2001
600
US
Is there anyway to have a script change a certain line number in a text file?

For example, I need it to change line 7 of c:\test.txt. So it will remove the text from line 7, and replace it with text that I specify.

thx
 
Here's a simple example of doing it manually. With a few snips here and there you should be able to figure out how to do it programmatically.
<%
const ForReading=1
const ForWriting=2
'***Replace &quot;YourTextFile.txt&quot; with the name of your file***
strTextFile=server.MapPath(&quot;YourTextFile.txt&quot;)
set fso = server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
%>
<form action=&quot;<%=request.ServerVariables(&quot;SCRIPT_NAME&quot;)%>&quot; method=&quot;post&quot; name=&quot;form1&quot;>
<textarea name=&quot;txtContent&quot; rows=&quot;10&quot; cols=&quot;70&quot; wrap=&quot;physical&quot;>
<%
if len(request.Form(&quot;btnUpdate&quot;)) then
NewText = request.Form(&quot;txtContent&quot;)
arrLines = split(NewText,vbcrlf)
set tStream=fso_OpenTextFile(strTextFile,ForWriting)
For x = 0 to UBound(arrLines)
ThisLine = arrLines(x)
if len(ThisLine) > 4 then
tStream.WriteLine mid(ThisLine,6)
end if
Next
tStream.close
End if

set tStream = fso.openTextFile(strTextFile,ForReading)
Do while not tStream.AtEndOfStream
LineNum=tStream.Line
strLine=Right(&quot;00&quot; & cstr(LineNum),3)
strText = tStream.Readline
response.Write(strLine & &quot;: &quot; & strText & vbcrlf)
loop
tStream.Close
%>
</textarea>
<%
if len(request.Form(&quot;btnUpdate&quot;)) then
NewText = request.Form(&quot;txtContent&quot;)
arrLines = split(NewText,vbcrlf)
set tStream=fso_OpenTextFile(strTextFile,ForWriting)
For x = 0 to UBound(arrLines)
ThisLine = arrLines(x)
if len(ThisLine) > 4 then
tStream.WriteLine mid(ThisLine,6)
end if
Next
tStream.close
End if
%>
<p><input type=&quot;submit&quot; name=&quot;btnUpdate&quot; value=&quot;Update&quot; ></p>


</form>

<%
set fso=nothing
%>
 
Brute force method in WSH:
Code:
Set fso=CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set ts=fso.OpenTextFile(&quot;c:\test.txt&quot;,1) 'ForReading
buf=ts.ReadAll: ts.Close
arr=Split(buf,vbCrLf)
arr(6)=&quot;New line#7&quot; ' Option Base 0
Set ts=fso.OpenTextFile(&quot;c:\test.txt&quot;,2) 'ForWriting
For i=0 To Ubound(arr)
  ts.WriteLine arr(i)
Next 'i
ts.Close

Hope This Help
PH.
 
PH, its giving me an error on your line, buf=ts.ReadAll

Do you know why?
 
Which error ?
You may try to replace the line with this:
Code:
buf=ts.ReadAll
ts.Close


Hope This Help
PH.
 
OK, i changed that, now its giving me an error on line 6 &quot;arr(6)=&quot;New line#7&quot;&quot;

Error - Subscript out of range: '[number:6]'
 
OK, i'm retarded. It would help if i actually had at least 7 lines in my text file. After putting 7 lines in the c:\test.txt, it worked great!

Thanks for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top