Solved

Use VB to edit text in a file

Posted on 2011-03-23
12
402 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 52

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
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 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Know what services you can and cannot, should and should not combine on your server.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

11 Experts available now in Live!

Get 1:1 Help Now