Solved

How to search in a file an item and change the settings using VBS??

Posted on 2015-02-14
5
118 Views
Last Modified: 2015-02-15
I want to look for an specify line and change the value from NO to YES.

For more info there is the attachment
0
Comment
Question by:acunaara
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 55

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 40610338
Give this a try I think it should do the job.  Save as a VBS file, and then run as either:

cscript EE28617184.vbs in.txt out.txt

or

cscript EE28617184.vbs in.txt

In the first case it will read the first file on the command line, make the changes, and then write to updated version to a new file named by the second parm.

In the second case it will apply the changes to the input file and overwrite it.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Define string to replace
strOldText = "AUTOMATIC_TRIAL = NO"
strNewText = "AUTOMATIC_TRIAL = YES"

' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
  sInfile = WScript.Arguments(0)
Else
  WScript.Echo "No filename specified."
  WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
  sOutfile = WScript.Arguments(1)
Else
  sOutfile = sInfile
End If

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Read entire input file into a variable and close it
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
sData = oInfile.ReadAll
oInfile.Close
Set oInfile = Nothing

sData = Replace(sData, vbCrLf, "<CRLF>")

' Write file with any changes made
Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)
oOutfile.Write(sData)
oOutfile.Close
Set oOutfile = Nothing

' Cleanup and end
Set oFSO = Nothing
' MsgBox "Conversion done."
Wscript.Quit

Open in new window

~bp
0
 
LVL 37

Expert Comment

by:bbao
ID: 40610339
you may use any plain text editor, including the Windows built-in one Notepad, to do that. just open the VBS file in text mode and use SEARCH & REPLACE function to get it done.

if you do prefer using some kind of VBS friendly editor, please check the below link for more information.

http://stackoverflow.com/questions/364283/what-is-your-favorite-vbscript-editor
0
 

Author Comment

by:acunaara
ID: 40610573
Thanks Bill

It works nice!!

The line 33 was wrong but i fixed it as follows:

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Define string to replace
strOldText = "AUTOMATIC_TRIAL = NO"
strNewText = "AUTOMATIC_TRIAL = YES"

' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
  sInfile = WScript.Arguments(0)
Else
  WScript.Echo "No filename specified."
  WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
  sOutfile = WScript.Arguments(1)
Else
  sOutfile = sInfile
End If

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Read entire input file into a variable and close it
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
sData = oInfile.ReadAll
oInfile.Close
Set oInfile = Nothing

sData = Replace(sData, strOldText, strNewText)

' Write file with any changes made
Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)
oOutfile.Write(sData)
oOutfile.Close
Set oOutfile = Nothing

' Cleanup and end
Set oFSO = Nothing
' MsgBox "Conversion done."
Wscript.Quit

Open in new window

0
 
LVL 55

Expert Comment

by:Bill Prew
ID: 40610932
Great, glad that helped.  Sorry about that bug, as you imagine I repurpose code when possible, forgot to adjust that line from a prior question.  Glad you sorted it out.

~bp
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Progress

726 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