Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Use VB to edit text in a file

Posted on 2011-03-23
12
Medium Priority
?
409 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 59

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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Remote Apps is a feature in server 2008 which allows users to run applications off Remote Desktop Servers without having to log into them to run the applications.  The user can either have a desktop shortcut installed or go through the web portal to…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

926 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