Solved

Use VB to edit text in a file

Posted on 2011-03-23
12
404 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Know what services you can and cannot, should and should not combine on your server.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

856 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