Dear All,

Pls help, I have a text file which I need to edit. The script should
search for a particular text using readline and then append a certain
text to that line.


The text file looks like


blah blah
blah blha
blah
happyb'day=gaurav
blah blah


It should look like this after modification
blah blah
blah blha
blah
happyb'day=gaurav,thanks
blah blah


P.S - The line # where "Happy'bday=gaurav" is not fixed.


Pls help, I need to get going on this and even a little insight will
be very helpful.


Thanks in advance
Gaurav

Re: Edit a txt file by Pegasus

Pegasus
Tue Oct 23 13:22:44 PDT 2007

Have a look here about multiposting vs. crossposting:
http://www.blakjak.demon.co.uk/mul_crss.htm


"Gaurav" <gaurav081@gmail.com> wrote in message
news:1193170228.945218.309080@e9g2000prf.googlegroups.com...
> Dear All,
>
> Pls help, I have a text file which I need to edit. The script should
> search for a particular text using readline and then append a certain
> text to that line.
>
>
> The text file looks like
>
>
> blah blah
> blah blha
> blah
> happyb'day=gaurav
> blah blah
>
>
> It should look like this after modification
> blah blah
> blah blha
> blah
> happyb'day=gaurav,thanks
> blah blah
>
>
> P.S - The line # where "Happy'bday=gaurav" is not fixed.
>
>
> Pls help, I need to get going on this and even a little insight will
> be very helpful.
>
>
> Thanks in advance
> Gaurav
>



Re: Edit a txt file by Owen

Owen
Tue Oct 23 13:33:26 PDT 2007

On Oct 23, 1:10 pm, Gaurav <gaurav...@gmail.com> wrote:
> Dear All,
>
> Pls help, I have a text file which I need to edit. The script should
> search for a particular text using readline and then append a certain
> text to that line.
>
> The text file looks like
>
> blah blah
> blah blha
> blah
> happyb'day=gaurav
> blah blah
>
> It should look like this after modification
> blah blah
> blah blha
> blah
> happyb'day=gaurav,thanks
> blah blah
>
> P.S - The line # where "Happy'bday=gaurav" is not fixed.
>
> Pls help, I need to get going on this and even a little insight will
> be very helpful.
>
> Thanks in advance
> Gaurav

google vbscript open text file for appending and you'll get hundreds
of examples of how to do this.


Re: Edit a txt file by ekkehard

ekkehard
Tue Oct 23 13:37:02 PDT 2007

Gaurav schrieb:
> Dear All,
>
> Pls help, I have a text file which I need to edit. The script should
> search for a particular text using readline and then append a certain
> text to that line.
>
>
> The text file looks like
>
>
> blah blah
> blah blha
> blah
> happyb'day=gaurav
> blah blah
>
>
> It should look like this after modification
> blah blah
> blah blha
> blah
> happyb'day=gaurav,thanks
> blah blah
>
>
> P.S - The line # where "Happy'bday=gaurav" is not fixed.
>
>
> Pls help, I need to get going on this and even a little insight will
> be very helpful.
>
>
> Thanks in advance
> Gaurav
>
One way to do it:

Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = ".\gaurav.txt"
Dim sFnd : sFnd = "happyb'day=gaurav"
Dim sRpl : sRpl = sFnd + ",thanks"

Dim sText
' read
sText= oFS.OpenTextFile( sFSpec ).ReadAll
' show
WScript.Echo sText
' change
sText = Replace( sText, sFnd, sRpl )
' show
WScript.Echo sText
' save
oFS.CreateTextFile( sFSpec, True ).Write sText

it assumes "Happy'bday=gaurav" occurs exactly as specified; ask again,
if the name could change ...

Re: Edit a txt file by Gaurav

Gaurav
Tue Oct 23 20:07:49 PDT 2007


Actually this is an ini file I am trying to update, sorry should have
mentioned this earlier.

happyb'day ..remains the same but it could take any value(like gaurav,
alex, john etc).

Hence the objective is to append the line having happy'bday with a
certain text(thanks in this case) at the end of it.



Re: Edit a txt file by Daniel

Daniel
Tue Oct 23 21:47:23 PDT 2007

On Oct 24, 1:07 pm, Gaurav <gaurav...@gmail.com> wrote:
> Actually this is an ini file I am trying to update, sorry should have
> mentioned this earlier.
>
> happyb'day ..remains the same but it could take any value(like gaurav,
> alex, john etc).
>
> Hence the objective is to append the line having happy'bday with a
> certain text(thanks in this case) at the end of it.

In that case split each line of the text (ini) file into an array and
then test each line for the trigger phrase in a loop. Append the text
to the end of the line and re-write the test file:-

Dim oFS : Set oFS =
CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = ".\gaurav.ini"
Dim sFnd : sFnd = "happyb'day="
Dim sText
'
Const FOR_WRITING = 2
' read
sText= oFS.OpenTextFile( sFSpec ).ReadAll
' show
WScript.Echo sText
' change
arrLines = Split(sText, VbNewLine)
Set objIniFile = objFSO.OpenTextFile(sFSpec,FOR_WRITING)
For Each strLine in arrLines
If InStr(strLine , sFind) Then
strNewLine = strLine & ",thanks."
objIniFile.WriteLine strNewLine
Else
objIniFile.WriteLine strLine
End If
Next
objIniFile.Close
Wscript.Echo "ini File updated!"
Wscript.Quit


Re: Edit a txt file by Gaurav

Gaurav
Wed Oct 24 13:42:56 PDT 2007

Thanks a lot for all your help guys. The below code is which I will be
finally using

Dim oFS : Set oFS =CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = "c:\notes\notes.ini"
Dim sFnd : sFnd = "PhoneLog="
Dim Sfnd2 : sFnd2 = ",thanks."
Dim sText
Dim Temp : Temp = "No"
'

Const FOR_WRITING = 2
Const For_Appending = 8
' read
sText= oFS.OpenTextFile(sFSpec).ReadAll
' show
'WScript.Echo sFnd
' change
arrLines = Split(sText, VbNewLine)
Set objIniFile = oFS.OpenTextFile(sFSpec,FOR_WRITING)
For Each strLine in arrLines
'wscript.echo InStr(strLine , sFnd)
If InStr(strLine , sFnd) Then
wscript.echo sfnd & "--> found in -->" & strline
Temp = "Yes"
if InStr(strLine , sFnd2) then
objIniFile.WriteLine strLine
wscript.echo sfnd2 & "--> also found in --
>" & strline
else
wscript.echo sfnd2 & "--> NOTNOTNOT found
in -->" & strline
strNewLine = strLine & ",thanks."
objIniFile.WriteLine strNewLine

End if

Else
objIniFile.WriteLine strLine
End If
Next
objIniFile.Close

If temp <> "Yes" then
wscript.echo "Phonelog doesn't exist"
Set objIniFile2 = oFS.OpenTextFile(sFSpec,For_Appending)
objIniFile2.WriteLine "PhoneLog=,thanks."
objIniFile2.close
end if
Wscript.Echo "ini File updated!"
Wscript.Quit