Replacing text within a pre-existing file 
Author Message
 Replacing text within a pre-existing file

I was hoping someone could send me a sample or point me in the right
direction.

I need a script that will open an existing file, find a certain line and
replace it with a value retrieved from user input.

For example, when the script runs it will prompt the user for some
information.
Later in the script it will use the information from those variables to edit
a text file that already exists.

Any help would be very appreciated.

Thanks,



Sat, 14 Sep 2002 03:00:00 GMT  
 Replacing text within a pre-existing file

To demo modifing a text file, I created this test file

echo 1 old text
echo 2 old text
echo 3 old text
pause
exit
----end---

Then this script file
---changeText.js----
var fso = new ActiveXObject("Scripting.FileSystemObject");
var ForReading = 1, ForWriting = 2, ForAppending = 8, f, f2, r;
f = fso.OpenTextFile("c:\\windows\\desktop\\oldtext.bat", ForReading);
f2 = fso.OpenTextFile("c:\\windows\\desktop\\oldtext2.bat", ForWriting, true);
while (!f.AtEndOfStream)  {
r =  f.ReadLine();
if(r == "echo 2 old text")
r = "REM " + r;
f2.WriteLine(r);

Quote:
}

f.Close();
f = fso.GetFile("c:\\windows\\desktop\\oldtext.bat");
f.Delete();
f2.Close();
f2 = fso.GetFile("c:\\windows\\desktop\\oldtext2.bat");
f2.Name = "oldtext.bat";
---end ---
(also attached)
--
Mark L. Ferguson  Please reply in newsgroup
marfer's notes for OE 5 > http://www.geocities.com/SiliconValley/Bay/6386/IE_ng_notes.htm
Quote:

> I was hoping someone could send me a sample or point me in the right
> direction.

> I need a script that will open an existing file, find a certain line and
> replace it with a value retrieved from user input.

> For example, when the script runs it will prompt the user for some
> information.
> Later in the script it will use the information from those variables to edit
> a text file that already exists.

> Any help would be very appreciated.

> Thanks,

  changetext.js
< 1K Download

  oldtext.bat
< 1K Download


Sat, 14 Sep 2002 03:00:00 GMT  
 Replacing text within a pre-existing file
This should give you a good start

File = "c:\data2.txt"
 strSearch = "stuff"

 UserInput = InputBox("What do you want to change  "&_
    vbCFLF & strSearch& " to","Enter Something")
 If UserInput = "" Then
  Wscript.Quit
 End If

 Set objFileSystem = CreateObject("Scripting.FileSystemObject")
 const ForReading = 1, ForWriting = 2
 Set oFile = objFileSystem.openTextFile(File, ForReading, True)
 arLineArray = Split(oFile.ReadAll, vbCRLF)
 oFile.Close
 MaxCount = Ubound(arLineArray)

 'Remove Empty lines from bottom of file (array)
 'moving from the end backward
 While arLineArray(MaxCount) = ""
  Redim Preserve arLineArray(MaxCount-1)
  MaxCount = MaxCount - 1
 Wend
 'If its found in the array it changes it to what the user entered
 For nCount = 0 to Ubound(arLineArray)
  If LCase(arLineArray(nCount)) = LCase(strSearch) Then
   arLineArray(nCount) = UserInput
  End If
 Next

 'rewrite the file with the change
 Set oFileOutPut = objFileSystem.openTextFile(File, ForWriting, True)
 oFileOutPut.WriteLine Join(arLineArray, vbCRLF)
 oFileOutPut.Close

--
David Hamel

NBT Inc.

Quote:
> I was hoping someone could send me a sample or point me in the right
> direction.

> I need a script that will open an existing file, find a certain line and
> replace it with a value retrieved from user input.

> For example, when the script runs it will prompt the user for some
> information.
> Later in the script it will use the information from those variables to
edit
> a text file that already exists.

> Any help would be very appreciated.

> Thanks,



Sat, 14 Sep 2002 03:00:00 GMT  
 Replacing text within a pre-existing file
Re all,

I have tried the examples here (thanks for the postings)
but i'm afraid i can't get them to work.

I too am trying to do a search and replace within a text
file.

Each line has something like:

TKELSF 31MAR2000 14.25

What I'm trying to do is rewrite the file searching for
"2000" in each line and replacing it with "00".

Could someone pls help?  Am a novice still, so a simple
vbs example would be the most helpful.

Humbly thankful...

Hank

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!



Tue, 17 Sep 2002 03:00:00 GMT  
 Replacing text within a pre-existing file
Using David's example as a starting point...

File = "c:\data2.txt"
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
const ForReading = 1, ForWriting = 2
Set oFile = objFileSystem.openTextFile(File, ForReading, True)
arLineArray = Split(oFile.ReadAll, vbCRLF)
oFile.Close

for i =  0 the ubound(arLineArray)
  arLineArray(i) = replace(arLineArray(i), "2000", "00")
next

Set oFileOutPut = objFileSystem.openTextFile(File, ForWriting, True)
oFileOutPut.WriteLine Join(arLineArray, vbCRLF)
oFileOutPut.Close

--
Michael Harris
MVP Scripting


Re all,

I have tried the examples here (thanks for the postings)
but i'm afraid i can't get them to work.

I too am trying to do a search and replace within a text
file.

Each line has something like:

TKELSF 31MAR2000 14.25

What I'm trying to do is rewrite the file searching for
"2000" in each line and replacing it with "00".

Could someone pls help?  Am a novice still, so a simple
vbs example would be the most helpful.

Humbly thankful...

Hank

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!



Tue, 17 Sep 2002 03:00:00 GMT  
 Replacing text within a pre-existing file
thanks very much for the reply, but i'm still having
a bit of a problem...

first off, the code i am using is...

File = "D:\FILE.TXT"
const ForReading = 1, ForWriting = 2
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set oFile = objFileSystem.openTextFile(File, ForReading, True)
arLineArray = Split(oFile.ReadAll, vbCRLF)
oFile.Close

for i = 0 to ubound(arLineArray)
arLineArray(i) = replace(arLineArray(i), "2000 ", "00 ")
next

Set oFileOutPut = objFileSystem.openTextFile(File, ForWriting,
True)
oFileOutPut.WriteLine Join(arLineArray, vbCRLF)
oFileOutPut.Close

the problem i am having is that it appends a extra line to the
bottom.  any advice on getting it to stop doing that?

tia...

best regards,

Hank

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!



Fri, 20 Sep 2002 03:00:00 GMT  
 Replacing text within a pre-existing file

Quote:

> thanks very much for the reply, but i'm still having
> a bit of a problem...

> first off, the code i am using is...

> File = "D:\FILE.TXT"
> const ForReading = 1, ForWriting = 2
> Set objFileSystem = CreateObject("Scripting.FileSystemObject")
> Set oFile = objFileSystem.openTextFile(File, ForReading, True)
> arLineArray = Split(oFile.ReadAll, vbCRLF)
> oFile.Close

> for i = 0 to ubound(arLineArray)
> arLineArray(i) = replace(arLineArray(i), "2000 ", "00 ")
> next

> Set oFileOutPut = objFileSystem.openTextFile(File, ForWriting,
> True)
> oFileOutPut.WriteLine Join(arLineArray, vbCRLF)
> oFileOutPut.Close

> the problem i am having is that it appends a extra line to the
> bottom.  any advice on getting it to stop doing that?

> tia...

> best regards,

> Hank

Just use Write in place of Writeline to suppress the extra newline.  You
are getting a newline from the vbCrLf in the Join and one extra from the
WriteLine method.

Or maybe use something like this ...

Set oFileOutPut = objFileSystem.openTextFile(File, ForWriting,True)

for i = 0 to ubound(arLineArray)
oFileOutPut.WriteLine replace(arLineArray(i), "2000 ", "00 ")
next

oFileOutPut.Close

Or maybe even ...

File = "D:\FILE.TXT"
const ForReading = 1, ForWriting = 2
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oInFile = oFS.openTextFile(File, ForReading, True)
sText = oFile.ReadAll
oInFile.Close

Set oOutFile = oFS.openTextFile(File, ForWriting, True)
oOutFile.Write replace(sText, "2000 ", "00 ")
oOutFile.Close

I don't quite see the need for parsing the file into lines.

Tom Lavedas
-----------
http://www.pressroom.com/~tglbatch/



Fri, 20 Sep 2002 03:00:00 GMT  
 Replacing text within a pre-existing file
File = "D:\FILE.TXT"
const ForReading = 1, ForWriting = 2
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set oFile = objFileSystem.openTextFile(File, ForReading, True)
arLineArray = Split(oFile.ReadAll, vbCRLF)
oFile.Close

'====
' if the last line in the file ends with a CR+LF pair,
' split creates an empty last array element
' because nothing follows the last delimiter.  The
' following tests for an empty last array element and
' drops it by resizing the array to be one element shorter.
'====
'
if IsEmpty(arLineArray(ubound(arLineArray))) then
    ReDim Preserve arLineArray(ubound(arLineArray)-1)
end if

for i = 0 to ubound(arLineArray)
arLineArray(i) = replace(arLineArray(i), "2000 ", "00 ")
next

Set oFileOutPut = objFileSystem.openTextFile(File, ForWriting,
True)
oFileOutPut.WriteLine Join(arLineArray, vbCRLF)
oFileOutPut.Close

--
Michael Harris
MVP Scripting


thanks very much for the reply, but i'm still having
a bit of a problem...

first off, the code i am using is...

File = "D:\FILE.TXT"
const ForReading = 1, ForWriting = 2
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set oFile = objFileSystem.openTextFile(File, ForReading, True)
arLineArray = Split(oFile.ReadAll, vbCRLF)
oFile.Close

for i = 0 to ubound(arLineArray)
arLineArray(i) = replace(arLineArray(i), "2000 ", "00 ")
next

Set oFileOutPut = objFileSystem.openTextFile(File, ForWriting,
True)
oFileOutPut.WriteLine Join(arLineArray, vbCRLF)
oFileOutPut.Close

the problem i am having is that it appends a extra line to the
bottom.  any advice on getting it to stop doing that?

tia...

best regards,

Hank

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!



Fri, 20 Sep 2002 03:00:00 GMT  
 Replacing text within a pre-existing file

Quote:

>File = "D:\FILE.TXT"
>const ForReading = 1, ForWriting = 2
>Set oFS = CreateObject("Scripting.FileSystemObject")
>Set oInFile = oFS.openTextFile(File, ForReading, True)
>sText = oFile.ReadAll
>oInFile.Close

>Set oOutFile = oFS.openTextFile(File, ForWriting, True)
>oOutFile.Write replace(sText, "2000 ", "00 ")
>oOutFile.Close

>I don't quite see the need for parsing the file into lines.

>Tom Lavedas

My apologies for leeching a bit here...  you know... VBScript
is not EXACTLY like vba...  and (maybe i'm wrong here) but
i'm almost positive i've read in here that you could paste
working code straight into a vbs file (obviously non-application
specific code) and expect it to work...  running into dead
ends...

The solution which continues to elude me is:

I am now simply trying to write a simple array that will
allow the above code to process file1.txt, file2.txt, and
file3.txt.

I have tried lots of variations based on an array that I used
in a VBA project, but can't get it to work.  Also... fwiw...
haven't seen a sample file on anyone's site that deals with
file input arrays.  <?>

Could someone pls post a simple example of the syntax.

Much oblige...

Hank

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!



Fri, 20 Sep 2002 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. How to search a text file and replace text

2. Replace text in a text file

3. Replace text sequnec in text files : how ?

4. RegExp Replace within replace

5. Replace characters within a file.

6. Replace Characters within a file.

7. replacing a string within a file

8. replace a string or line within ini file

9. replacing text in a html-file and saving the changed file

10. Trying to search and replace a text file for a marked point in ht m file

11. Insertion of text file into existing PS file

12. Replacing text marks in a postscript file

 

 
Powered by phpBB® Forum Software