I have to make an edit to a file and what I was planning to do is open
the file to read, then compare each line read to see if it equals my
search value, which is a substring of what is probably there. If it
does not, simply write it to another file. If it does = my search,
simply replace the value with my edit and write to the other file. I
would do this until the original file is at end. I would then like to
move my new file over the original, which should now include my edit.

Or

Open File for Read
Open Other File for Right

'read through first file
While file not at end
If line contains my search substring
replace line to include new substring
write it to other file
else
write line to other file
End if
End While

Close new file
Close original file

Move new file to original

Done!


Any guidance from a vbscript wiz would be greatly appreciated!

Thanks!!!

Re: Read from one Text file and Write to another. by Adam

Adam
Thu Jun 14 15:08:02 CDT 2007

On Jun 14, 12:33 pm, Superfreak3 <Matt.Wal...@synergis.com> wrote:
> I have to make an edit to a file and what I was planning to do is open
> the file to read, then compare each line read to see if it equals my
> search value, which is a substring of what is probably there. If it
> does not, simply write it to another file. If it does = my search,
> simply replace the value with my edit and write to the other file. I
> would do this until the original file is at end. I would then like to
> move my new file over the original, which should now include my edit.
>
> Or
>
> Open File for Read
> Open Other File for Right
>
> 'read through first file
> While file not at end
> If line contains my search substring
> replace line to include new substring
> write it to other file
> else
> write line to other file
> End if
> End While
>
> Close new file
> Close original file
>
> Move new file to original
>
> Done!
>
> Any guidance from a vbscript wiz would be greatly appreciated!
>
> Thanks!!!

For searching for a substring, try using InStr (http://
ns7.webmasters.com/caspdoc/html/vbscript_instr_function.htm). Len,
LTrim, RTrim, Right, and Left can also be useful when parsing files.
If you want to get really elegant, try looking at regular expressions
(http://www.regular-expressions.info/vbscript.html).

Your pseudocode above already has the logic for what you want to do.
And now that you know the methods for searching for a substring all
you need to do is match code to it!


Re: Read from one Text file and Write to another. by noone

noone
Thu Jun 14 16:43:42 CDT 2007

Il giorno Thu, 14 Jun 2007 11:33:50 -0700, Superfreak3 <Matt.Walker@synergis.com> ha
scritto:

>I have to make an edit to a file and what I was planning to do is open
>the file to read, then compare each line read to see if it equals my
>search value, which is a substring of what is probably there. If it
>does not, simply write it to another file. If it does = my search,
>simply replace the value with my edit and write to the other file. I
>would do this until the original file is at end. I would then like to
>move my new file over the original, which should now include my edit.

You can start on this
The script copies agent.ini to agent.ini.bak,
then opens the bak and copies every line to agent.ini
except for the line containing "newsserver", which has to be changed.
It is part of an HTA I use to easily change the forte agent news server
when I use a different connection.

Const ForReading = 1 'Open a file for reading only. You can't write to this file
Const ForWriting = 2 'Open a file for writing
q= chr(34) 'virgolette
Set fso = CreateObject("Scripting.FileSystemObject")
on error resume next
fso.DeleteFile "AGENT.INI.BAK"
on error goto 0
fso.MoveFile "AGENT.INI","AGENT.INI.BAK"
set source = fso.OpenTextFile("AGENT.INI.BAK",ForReading)
set dest= fso.OpenTextFile("AGENT.INI",ForWriting,TRUE)
Do While source.AtEndOfStream <> True
riga = source.ReadLine
if instr(1,riga,"NewsServer=")>0 then
dest.writeline "NewsServer=" & q & select1.value & q
else
dest.writeline riga
end if
Loop
source.Close
dest.close
window.close

--
Giovanni Cenati (Aosta, Italy)
Write to user "Reventlov" and domain at katamail com
http://digilander.libero.it/Cenati (Esempi e programmi in VbScript)
--

Re: Read from one Text file and Write to another. by Superfreak3

Superfreak3
Fri Jun 15 12:24:15 CDT 2007

On Jun 14, 5:43 pm, n...@no.void (Reventlov) wrote:
> Il giorno Thu, 14 Jun 2007 11:33:50 -0700,Superfreak3<Matt.Wal...@synergis.com> ha
> scritto:
>
> >I have to make an edit to a file and what I was planning to do is open
> >the file to read, then compare each line read to see if it equals my
> >search value, which is a substring of what is probably there. If it
> >does not, simply write it to another file. If it does = my search,
> >simply replace the value with my edit and write to the other file. I
> >would do this until the original file is at end. I would then like to
> >move my new file over the original, which should now include my edit.
>
> You can start on this
> The script copies agent.ini to agent.ini.bak,
> then opens the bak and copies every line to agent.ini
> except for the line containing "newsserver", which has to be changed.
> It is part of an HTA I use to easily change the forte agent news server
> when I use a different connection.
>
> Const ForReading = 1 'Open a file for reading only. You can't write to this file
> Const ForWriting = 2 'Open a file for writing
> q= chr(34) 'virgolette
> Set fso = CreateObject("Scripting.FileSystemObject")
> on error resume next
> fso.DeleteFile "AGENT.INI.BAK"
> on error goto 0
> fso.MoveFile "AGENT.INI","AGENT.INI.BAK"
> set source = fso.OpenTextFile("AGENT.INI.BAK",ForReading)
> set dest= fso.OpenTextFile("AGENT.INI",ForWriting,TRUE)
> Do While source.AtEndOfStream <> True
> riga = source.ReadLine
> if instr(1,riga,"NewsServer=")>0 then
> dest.writeline "NewsServer=" & q & select1.value & q
> else
> dest.writeline riga
> end if
> Loop
> source.Close
> dest.close
> window.close
>
> --
> Giovanni Cenati (Aosta, Italy)
> Write to user "Reventlov" and domain at katamail comhttp://digilander.libero.it/Cenati(Esempi e programmi in VbScript)
> --

PERFECT! It worked like a charm for my purposes.

THANKS!!