Solved

Use VB to edit text in a file

Posted on 2011-03-23
12
403 Views
Last Modified: 2012-05-11
Hello Experts,

I have been searching to find a vb script which will allow me to essentially do a find/replace on a file (xml)

I've come across a lot of documentation, but i'm piecing things together as there seems to be nothing like this on google, etc.

Here is a link with lots of info, but none really useful:
http://www.bigresource.com/VB-I-just-want-to-replace-one-sentence-in-a-text-file--NSsZ5oWQy0.html#BxTOqLIPGx

Can somebody point me into the right direction?
Dim FileName, Find, ReplaceWith, FileContents, dFileContents
Find         = WScript.Arguments(server1)
ReplaceWith  = WScript.Arguments(server2)
FileName     = WScript.Arguments(figgy.xml)

'Read source text file
FileContents = GetFile(FileName)

'replace all string In the source file
dFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 1)

Open in new window

0
Comment
Question by:zequestioner
12 Comments
 
LVL 1

Author Comment

by:zequestioner
ID: 35202853
Here is another good article, but I can't seem to get it to work quite right.

http://www.motobit.com/tips/detpg_replfile/
0
 
LVL 8

Expert Comment

by:subhashchy
ID: 35202893
can you try this....


Const ForReading = 1
Const ForWriting = 2

strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)

Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close

Open in new window




the parameter is cscrript script name filename(XML)  search_String replace_string
example would be ..

replace.vbs test1.txt findthis replacewiththis

Open in new window


0
 
LVL 1

Author Comment

by:zequestioner
ID: 35203063
I would ultimately like the 'ReplaceWithThis' to be set by the user.

Is there a way to use a variable for this? For example:

strReplaceWithText= InputBox("Enter the Replacement Text")
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 8

Expert Comment

by:subhashchy
ID: 35203098
yup..Modify the StrnewText line with your code.

I will place the full code here...

Const ForReading = 1
Const ForWriting = 2

strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strnewText= InputBox("Enter the Replacement Text")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)

Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close

Open in new window


SO, the new argumetns will be filename Old text and enter
it will popup the inputbox
0
 
LVL 6

Expert Comment

by:Draxonic
ID: 35203106
This might help point you in the right direction:

http://draxonic.blogspot.com/2009/09/how-to-edit-ini-file-using-vbscript.html
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 35203130
How will this be used, there are a lot of GUI search and replace tools already out there.  I wonder if VBS is the best choice?

Also, what are the types of strings that you would typically be replacing, are they very simple things, or somewhat complex?

~bp
0
 
LVL 1

Author Comment

by:zequestioner
ID: 35203154
It errors out on this line:

Set objFile = objFSO.OpenTextFile(strFileName, ForReading)

See Error below error error
strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strnewText= InputBox("Enter the Replacement Text")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)

Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close

Open in new window

0
 
LVL 8

Accepted Solution

by:
subhashchy earned 500 total points
ID: 35203175
i tried copy pasting from above code box and it did errored out..however when i check the original code it works fine..


can you make sure of any type copy this ..

Const ForReading = 1
Const ForWriting = 2
strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strnewText= InputBox("Enter the Replacement Text")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)

Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close

Open in new window

0
 
LVL 1

Author Comment

by:zequestioner
ID: 35203180
I am using a .bat to execute this:

ReplaceDsName.vbs configuration.xml brainstate3

I notice if I add the cscript:

cscript ReplaceDsName.vbs configuration.xml oldtext

then it prompts me for NewText and does not error out.... however, the OldText is not replaced with the NewText
0
 
LVL 8

Expert Comment

by:subhashchy
ID: 35203254
on my system i did executed it from a batch and seems to be working..Not sure what could be the reason..can you post your batch and xml if possible
0
 
LVL 1

Author Comment

by:zequestioner
ID: 35203338
sorry, i made that last post before i read yours. Since i copied and pasted identically, it seems to be working like a charm. Thanks so much for your help!
0
 
LVL 8

Expert Comment

by:subhashchy
ID: 35203347
you welcome..
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome to my series of short tips on migrations. Whilst based on Microsoft migrations the same principles can be applied to any type of migration. My first tip is around source server preparation. No migration is an easy migration, there is a…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question