?
Solved

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

Posted on 2015-02-14
5
Medium Priority
?
119 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 56

Accepted Solution

by:
Bill Prew earned 2000 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 56

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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?
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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