Solved

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

Posted on 2015-02-14
5
111 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
  • 2
5 Comments
 
LVL 53

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 53

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

809 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