replacing a string within a file 
Author Message
 replacing a string within a file

I would like to run a script from cmd line and enter 1 argument, pass that
to an open file object replacing a predefined text string. I've gone through
the VBScript docs, ADSI docs, newsgroups, Tim Hill's WSH book etc.. I can't
find any examples of this. I've found info about doing a seach and .Replace
by entering 2 cmdline args. My appologies if I missed it in any of these
resources.
I've tried numerous ways to get this to work...still stuck! I think the
closest I've come is with the script below. This doesn't return any errors,
but doesn't get the job done either. Any help would be greatly appreciated.
-pbruno

dim a, b
set a = CreateObject("Scripting.FileSystemObject")
set b = a.OpenTextFile("d:\test.txt", 8)

Test(Wscript.Arguments(0))

Function Test(NewText)
 Old = "text-to-replace"
 Wscript.Echo "Replacing ", old, " with ", NewText
 Set x = New RegExp
 x.Global = True
 x.Pattern = "([A-Za-z] \w*)"
 x.Replace Old, NewText
End Function



Tue, 28 Jan 2003 03:00:00 GMT  
 replacing a string within a file
dim a, b
set a = CreateObject("Scripting.FileSystemObject")
set b = a.OpenTextFile("c:\_temp\test.txt", 1)
fileTxt = b.ReadAll
b.Close
oldTxt = "text-to-replace"
newTxt = Wscript.Arguments(0)
fileTxt = Replace(fileTxt,oldTxt,newTxt)
set b = a.OpenTextFile("c:\_temp\test.txt", 2)
b.write fileTxt
b.Close

--
Michael Harris
Microsoft MVP - Scripting

"It always helps to read the documentation..."

Quote:

> I would like to run a script from cmd line and enter 1 argument, pass that
> to an open file object replacing a predefined text string. I've gone through
> the VBScript docs, ADSI docs, newsgroups, Tim Hill's WSH book etc.. I can't
> find any examples of this. I've found info about doing a seach and .Replace
> by entering 2 cmdline args. My appologies if I missed it in any of these
> resources.
> I've tried numerous ways to get this to work...still stuck! I think the
> closest I've come is with the script below. This doesn't return any errors,
> but doesn't get the job done either. Any help would be greatly appreciated.
> -pbruno

> dim a, b
> set a = CreateObject("Scripting.FileSystemObject")
> set b = a.OpenTextFile("d:\test.txt", 8)

> Test(Wscript.Arguments(0))

> Function Test(NewText)
>  Old = "text-to-replace"
>  Wscript.Echo "Replacing ", old, " with ", NewText
>  Set x = New RegExp
>  x.Global = True
>  x.Pattern = "([A-Za-z] \w*)"
>  x.Replace Old, NewText
> End Function



Tue, 28 Jan 2003 03:00:00 GMT  
 replacing a string within a file

Quote:

> I would like to run a script from cmd line and enter 1 argument, pass that
> to an open file object replacing a predefined text string. I've gone through
> the VBScript docs, ADSI docs, newsgroups, Tim Hill's WSH book etc.. I can't
> find any examples of this. I've found info about doing a seach and .Replace
> by entering 2 cmdline args. My appologies if I missed it in any of these
> resources.
> I've tried numerous ways to get this to work...still stuck! I think the
> closest I've come is with the script below. This doesn't return any errors,
> but doesn't get the job done either. Any help would be greatly appreciated.
> -pbruno

> dim a, b
> set a = CreateObject("Scripting.FileSystemObject")
> set b = a.OpenTextFile("d:\test.txt", 8)

> Test(Wscript.Arguments(0))

> Function Test(NewText)
>  Old = "text-to-replace"
>  Wscript.Echo "Replacing ", old, " with ", NewText
>  Set x = New RegExp
>  x.Global = True
>  x.Pattern = "([A-Za-z] \w*)"
>  x.Replace Old, NewText
> End Function

Where do you read the text out of the file and where do you write it
back again?  Oh, and your function doesn't return any results and you
don't do anything with the result, anyway.  

Is Old supposed to contain the text read from the file?  Then I think
you want to ReadAll the text from the file into it.  You'll need to open
the file twice, once for read, then close it and reopen it for Write.
The Append mode is just that, for adding text on the end.  You can't do
a random operation on any files with script.  The FSO doesn't support
it.

I don't know regular expressions, so I can't comment on the accuracy of
the Replace, but the stucture you're looking for is something like this
...

set a = CreateObject("Scripting.FileSystemObject")
set b = a.OpenTextFile("d:\test.txt", 1)

Old = b.ReadAll
b.Close
set b = a.OpenTextFile("d:\test.txt", 2)
a.Write Test(Old, Wscript.Arguments(0))

Function Test(Old, NewText)
 Wscript.Echo "Replacing ", old, " with ", NewText
 Set x = New RegExp
 x.Global = True
 x.Pattern = "([A-Za-z] \w*)"
 x.Replace Old, NewText
 Test = Old
End Function

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



Tue, 28 Jan 2003 03:00:00 GMT  
 replacing a string within a file
Thanks for both responses......Mike this one seems to fit the bill.

Quote:
> dim a, b
> set a = CreateObject("Scripting.FileSystemObject")
> set b = a.OpenTextFile("c:\_temp\test.txt", 1)
> fileTxt = b.ReadAll
> b.Close
> oldTxt = "text-to-replace"
> newTxt = Wscript.Arguments(0)
> fileTxt = Replace(fileTxt,oldTxt,newTxt)
> set b = a.OpenTextFile("c:\_temp\test.txt", 2)
> b.write fileTxt
> b.Close

> --
> Michael Harris
> Microsoft MVP - Scripting

> "It always helps to read the documentation..."




- Show quoted text -

Quote:
> > I would like to run a script from cmd line and enter 1 argument, pass
that
> > to an open file object replacing a predefined text string. I've gone
through
> > the VBScript docs, ADSI docs, newsgroups, Tim Hill's WSH book etc.. I
can't
> > find any examples of this. I've found info about doing a seach and
.Replace
> > by entering 2 cmdline args. My appologies if I missed it in any of these
> > resources.
> > I've tried numerous ways to get this to work...still stuck! I think the
> > closest I've come is with the script below. This doesn't return any
errors,
> > but doesn't get the job done either. Any help would be greatly
appreciated.
> > -pbruno

> > dim a, b
> > set a = CreateObject("Scripting.FileSystemObject")
> > set b = a.OpenTextFile("d:\test.txt", 8)

> > Test(Wscript.Arguments(0))

> > Function Test(NewText)
> >  Old = "text-to-replace"
> >  Wscript.Echo "Replacing ", old, " with ", NewText
> >  Set x = New RegExp
> >  x.Global = True
> >  x.Pattern = "([A-Za-z] \w*)"
> >  x.Replace Old, NewText
> > End Function



Sat, 01 Feb 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Replacing a string within a string?

2. replace a string or line within ini file

3. RegExp Replace within replace

4. Replace characters within a file.

5. Replace Characters within a file.

6. Replacing text within a pre-existing file

7. replace strings/text in EPS files

8. Replace a string in a txt file

9. Replace string value in a text file

10. how to replace string in big file?

11. Searching for a string of text within a file on remote users computers

12. RegExp: replace pattern not contained within quotes

 

 
Powered by phpBB® Forum Software