Reputation: 21
Below is part of a script I already wrote:
For i = 1 to arrsize
strText=""
Set objFile = objFSO.OpenTextFile(filetable(i), ForReading)
wscript.echo filetable(i)
On Error Resume Next
Do Until objFile.AtEndOfStream
strline = objFile.ReadLine
If instr(strline, source_string) > 0 Then
wscript.echo strline
Do Until instr(strline, " TEXT ") > 0 or instr(strline, Chr(9) & "TEXT ")
strline = objFile.ReadLine
Loop
strNewText = Replace(strline, "TEXT " & Chr(34), "TEXT " & Chr(34) & "Critical: ",1,-1,0)
wscript.echo strNewText
'up to here everything is OK
Set objFile = objFSO.OpenTextFile(filetable(i), ForWriting)
objFile.WriteLine strNewText
objFile.close
'The three line above are overwriting the text file
End If`enter code here`
Loop
Next
My script is to search in an array of files for a specific string registered in source_string variable, then if it matches it should search for "TEXT" word preceded by space or tab, when matches and it should it will then replace the line with the same line as per the following format: TEXT "Critical: ****". I hope it is clear and thanks in advanced
Upvotes: 1
Views: 670
Reputation: 21
For i = 1 to arrsize
Set objFile = objFSO.OpenTextFile(filetable(i), ForReading)
wscript.echo filetable(i)
On Error Resume Next
Dim filearray()
Linenb = 0
Do Until objFile.AtEndOfStream
Linenb = Linenb + 1
Redim Preserve filearray(Linenb)
filearray(Linenb) = objFile.ReadLine
' wscript.echo filearray(Linenb)
If instr(filearray(Linenb), source_string) > 0 Then
' wscript.echo filearray(Linenb)
Do Until instr(filearray(Linenb), " TEXT ") > 0 or instr(filearray(Linenb), Chr(9) & "TEXT ")
Linenb = Linenb + 1
Redim Preserve filearray (Linenb)
filearray(Linenb) = objFile.ReadLine
' wscript.echo filearray(Linenb)
Loop
filearray(Linenb) = Replace(filearray(Linenb), "TEXT " & Chr(34), "TEXT " & Chr(34) & "Critical: ",1,-1,0)
' wscript.echo filearray(Linenb)
End If
Loop
For j = 1 to Linenb
Set objFile = objFSO.OpenTextFile(filetable(i), ForWriting)
objfile.WriteLine filearray(j)
Next
Redim Preserve filearray (1)
Next
Upvotes: 0
Reputation: 396
I think here you open the text file again and you start at the start of stream and thus the first line (and not the found line) :
Set objFile = objFSO.OpenTextFile(filetable(i), ForWriting)
objFile.WriteLine strNewText
objFile.close
Maybe you should try opening the file for reading&writing, readline, when you str is found, then change that line and move on ...
Upvotes: 1