Solved

Use VB to edit text in a file

Posted on 2011-03-23
12
401 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
 
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 51

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Know what services you can and cannot, should and should not combine on your server.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now