Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBScript - replacing text in a text file

Posted on 2003-03-13
8
Medium Priority
?
653 Views
Last Modified: 2013-12-26
What I am looking to do is replace sid1/sid2/sid3 in a text file with the value of a variable sid01, sid02, or sid03.

Example:

  Contents of text file:

seinteractivelogonright = *S-1-5-32-544,*sid1,*sid2
seloaddriverprivilege = *S-1-5-32-544
selockmemoryprivilege =
senetworklogonright = *S-1-5-32-544,*S-1-5-11,*S-1-5-9,*S-1-1-0,*S-1-5-32-545
seprofilesingleprocessprivilege = *S-1-5-32-544
seremoteshutdownprivilege = *S-1-5-32-544
serestoreprivilege = *S-1-5-32-544,*sid2
sesecurityprivilege = *S-1-5-32-544
seservicelogonright = *S-1-5-32544,*sid1,*sid2,*sid3,SYSTEM


Question: What is the syntax of a replace (or regex) function that will search all strings in a text file, so I can replace sid(1/2/3) irregardless of the string it is a part of? Is there a wildcard character?




0
Comment
Question by:tnapolitano
[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
  • 5
  • 2
8 Comments
 
LVL 3

Author Comment

by:tnapolitano
ID: 8130063
Here is my script so far:

Option Explicit
'Set Variables
Dim objItem, colItems, objWMIService, strComputer, Username1, Username2, Username3, Username4,
fso, fsoFile, objFSO, objFile
 

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_UserAccount",,48)
    UserName1 = InputBox("Enter the account name of the Exchange Service account. The default is  

       ExchangeService:","Input Box","ExchangeService")
    UserName2 = InputBox("Enter the account name of the MailRoom account. The default is          

      MailRoom:","Input Box","MailRoom")
    Username3 = InputBox("Enter the account name of the Decision Agent Administrator account. The

       default is DAAdministrator:","Input Box","DAAdministrator")
    Username4 = InputBox("Enter the account name of the Message Center account. The default is    

       msgcntr:","Input Box","msgcntr")

Dim sid01
Dim sid02
Dim sid03
Dim sid04

'(Please comment on code below. How do I pass the value of the variable to the text line to be replaced, rather than the variable name? Will this work as it is?)

For Each objItem in colItems
  If objItem.Name = UserName1 then
      sid01 = objItem.SID
  ElseIf objItem.Name = UserName2 then
      sid02 = objItem.SID
  ElseIf objItem.Name = UserName3 then
      sid03 = objItem.SID
  ElseIf objItem.Name = UserName4 then
      sid04 = objItem.SID

  End If
Next



0
 
LVL 11

Accepted Solution

by:
rdrunner earned 500 total points
ID: 8135734
You can do that with the regexp object

Try this code snippet

'--------- CODE
Private Sub Command2_Click()

Dim oRegExp As New RegExp
Dim oMatches As MatchCollection
Dim oMatch As Match
Dim oInput As TextStream
Dim cLine As String

oRegExp.IgnoreCase = True
oRegExp.Global = True
oRegExp.MultiLine = True
oRegExp.Pattern = "(sid)([123])"

Set oInput = ofso.OpenTextFile("c:\work\test.txt")
cLine = oInput.ReadAll
cLine = oRegExp.Replace(cLine, "*I was the Number $2 Sith Lord*")
Debug.Print cLine
End Sub
'---------

'------DEBUG Output

seinteractivelogonright = *S-1-5-32-544,**I was the Number 1 Sith Lord*,**I was the Number 2 Sith Lord*
seloaddriverprivilege = *S-1-5-32-544
selockmemoryprivilege =
senetworklogonright = *S-1-5-32-544,*S-1-5-11,*S-1-5-9,*S-1-1-0,*S-1-5-32-545
seprofilesingleprocessprivilege = *S-1-5-32-544
seremoteshutdownprivilege = *S-1-5-32-544
serestoreprivilege = *S-1-5-32-544,**I was the Number 2 Sith Lord*
sesecurityprivilege = *S-1-5-32-544
seservicelogonright = *S-1-5-32544,**I was the Number 1 Sith Lord*,**I was the Number 2 Sith Lord*,**I was the Number 3 Sith Lord*,SYSTEM

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:tnapolitano
ID: 8137763
rdrunner -

Thanks for the help. But I actually got my replace function working last night. ended up using a nested replace [Replace(Replace(Replace(fContents, "sid1", sid01), "sid2", sid02), "sid3", sid03)].

Anyway, thanks for taking the time to answer. I'm not sure yet how this points system works, so if I owe you any for your suggestion, let me know.

Todd
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 8137840
Well if you like an answer here you grade it...

Then the poster of the answer gets some "expert points" (those are no question points) -
For an A he gets
Question * 4
for a C he gets
Question * 2

If you solved your question yourself then you can post a question in community support for a refund (include link to question)... I dont mind that but most ppl dont like if they answered a question and the asker gets a refund... (you need to make like 3000 Expert points a month for a free subscription)

Also if someone sanswers your question and it is solved grade it so everyone knows its no open anymore and tries to answer it...

Hope this help

0
 
LVL 3

Author Comment

by:tnapolitano
ID: 8138766
rdrunner -

I spoke too soon. Good thing I didnt close this out.

I moved my replace function over to my working script, and now it is clearing out everything in the text file.

here is the script. any suggestions.

Option Explicit

Dim objItem, colItems, objWMIService, strComputer, Username1, Username2, Username3, Username4, objFile, fsoTextStream, fsoFileContents, strOldC2Dc, fs, f, ts, fsTextStream, fContents, Groupname, objItem2, colItems2

   

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_UserAccount",,48)
    UserName1 = InputBox("Enter the account name of the Exchange Service account. The default is         ExchangeService:","Input Box","ExchangeService")
    UserName2 = InputBox("Enter the account name of the MailRoom account. The default is MailRoom:","Input         Box","MailRoom")
    Username3 = InputBox("Enter the account name of the Decision Agent Administrator         account. The default is DAAdministrator:","Input Box","DAAdministrator")
    Username4 = InputBox("Enter the account name of the Message Center account. The default         is msgcntr:","Input Box","msgcntr")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Group",,48)
Groupname = InputBox("Enter the Group name of the DA Proxy Users Group account. The default is         Da Proxy Users:","Input Box","Da Proxy Users")


Dim sid01, sid02, sid03, sid04, sid05


For Each objItem in colItems
  If objItem.Name = UserName1 then
      sid01 = objItem.SID
WScript.Echo sid01
   ElseIf objItem.Name = UserName2 then
      sid02 = objItem.SID
WScript.Echo sid02
   ElseIf objItem.Name = UserName3 then
      sid03 = objItem.SID
WScript.Echo sid03
   ElseIf objItem.Name = UserName4 then
      sid04 = objItem.SID
WScript.Echo sid04
   ElseIf objItem.Name = Groupname then
      sid05 = objItem.SID
WScript.Echo sid05

   End If
Next

Const ForReading = 1
Const ForWriting = 2


Set fs = CreateObject("Scripting.FileSystemObject")

'Set f = fs.GetFile("d:\c2_dc.txt")

Set f = fs.OpenTextFile("d:\c2_dc.txt", ForReading)

fContents = f.ReadAll

f.Close

Function ReplaceSID(strPattern, replString)

Dim regEx

Set regEx = New RegExp

regEx.Global = True

regEx.IgnoreCase = True

regEx.Pattern = "sid[12345]"

ReplaceSID = Replace(Replace(Replace(Replace(fContents, "sid1", sid01), "sid2", sid02), "sid3", sid03), "sid5", sid05)

Set fsTextStream = fs.OpenTextFile("d:\c2_dc.txt", ForWriting)

fsTextStream.Write ReplaceSID

fsTextStream.Close

End Function

MsgBox(ReplaceSID(fContents, "sid1"))


0
 
LVL 3

Author Comment

by:tnapolitano
ID: 8138769
rdrunner -

I spoke too soon. Good thing I didnt close this out.

I moved my replace function over to my working script, and now it is clearing out everything in the text file.

here is the script. any suggestions.

Option Explicit

Dim objItem, colItems, objWMIService, strComputer, Username1, Username2, Username3, Username4, objFile, fsoTextStream, fsoFileContents, strOldC2Dc, fs, f, ts, fsTextStream, fContents, Groupname, objItem2, colItems2

   

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_UserAccount",,48)
    UserName1 = InputBox("Enter the account name of the Exchange Service account. The default is         ExchangeService:","Input Box","ExchangeService")
    UserName2 = InputBox("Enter the account name of the MailRoom account. The default is MailRoom:","Input         Box","MailRoom")
    Username3 = InputBox("Enter the account name of the Decision Agent Administrator         account. The default is DAAdministrator:","Input Box","DAAdministrator")
    Username4 = InputBox("Enter the account name of the Message Center account. The default         is msgcntr:","Input Box","msgcntr")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Group",,48)
Groupname = InputBox("Enter the Group name of the DA Proxy Users Group account. The default is         Da Proxy Users:","Input Box","Da Proxy Users")


Dim sid01, sid02, sid03, sid04, sid05


For Each objItem in colItems
  If objItem.Name = UserName1 then
      sid01 = objItem.SID
WScript.Echo sid01
   ElseIf objItem.Name = UserName2 then
      sid02 = objItem.SID
WScript.Echo sid02
   ElseIf objItem.Name = UserName3 then
      sid03 = objItem.SID
WScript.Echo sid03
   ElseIf objItem.Name = UserName4 then
      sid04 = objItem.SID
WScript.Echo sid04
   ElseIf objItem.Name = Groupname then
      sid05 = objItem.SID
WScript.Echo sid05

   End If
Next

Const ForReading = 1
Const ForWriting = 2


Set fs = CreateObject("Scripting.FileSystemObject")

'Set f = fs.GetFile("d:\c2_dc.txt")

Set f = fs.OpenTextFile("d:\c2_dc.txt", ForReading)

fContents = f.ReadAll

f.Close

Function ReplaceSID(strPattern, replString)

Dim regEx

Set regEx = New RegExp

regEx.Global = True

regEx.IgnoreCase = True

regEx.Pattern = "sid[12345]"

ReplaceSID = Replace(Replace(Replace(Replace(fContents, "sid1", sid01), "sid2", sid02), "sid3", sid03), "sid5", sid05)

Set fsTextStream = fs.OpenTextFile("d:\c2_dc.txt", ForWriting)

fsTextStream.Write ReplaceSID

fsTextStream.Close

End Function

MsgBox(ReplaceSID(fContents, "sid1"))


0
 
LVL 3

Author Comment

by:tnapolitano
ID: 8139276
Thanks for your help.
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

704 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