Find & Replace script – Expanded

Home Forums Scripting Windows Script Host Find & Replace script – Expanded

This topic contains 2 replies, has 1 voice, and was last updated by CaTBuS CaTBuS 8 years, 2 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • CaTBuS
    CaTBuS
    Participant
    #155145

    I have the following Find/Replace script and it works fine. What I’m wanting to do and am unable to figure out is how to expand the script to find and replace multiple strings. I am still learning am am certain that I’m missing some simple bit of basic know-how. Anyway, here is what I have:

    Code:
    [COLOR=”Blue”]Const[/COLOR] ForReading = [COLOR=”Red”]1[/COLOR]
    [COLOR=”blue”]Const[/COLOR] ForWriting = [COLOR=”red”]2[/COLOR]

    [COLOR=”blue”]Set[/COLOR] objFSO = CreateObject(“[COLOR=”Gray”]Scripting.FileSystemObject[/COLOR]”)
    [COLOR=”blue”]Set[/COLOR] objFile = objFSO.OpenTextFile(“[COLOR=”Gray”]C:ScriptsTest.txt[/COLOR]”, ForReading)

    strText1 = objFile.ReadAll
    objFile.Close
    strNewText1 = Replace(strText1, “[COLOR=”gray”]server.location1″, “server[/COLOR]”)

    [COLOR=”blue”]Set[/COLOR] objFile = objFSO.OpenTextFile(“[COLOR=”gray”]C:ScriptsTest.txt[/COLOR]”, ForWriting)
    objFile.WriteLine strNewText1
    objFile.Close

    Here is one of the many ways I have tried to do this to no avail. In addition to assistance with the actual correct code, I would love an explanation as to why my logic doesn’t work. Thank you in advance:

    Code:
    [COLOR=”Blue”]Const[/COLOR] ForReading = [COLOR=”Red”]1[/COLOR]
    [COLOR=”blue”]Const[/COLOR] ForWriting = [COLOR=”red”]2[/COLOR]

    [COLOR=”blue”]Set[/COLOR] objFSO = CreateObject(“[COLOR=”Gray”]Scripting.FileSystemObject[/COLOR]”)
    [COLOR=”blue”]Set[/COLOR] objFile = objFSO.OpenTextFile(“[COLOR=”Gray”]C:ScriptsTest.txt[/COLOR]”, ForReading)

    strText1 = objFile.ReadAll
    objFile.Close
    strNewText1 = Replace(strText1, “[COLOR=”gray”]server.location1″, “server[/COLOR]”)

    [COLOR=”blue”]Set[/COLOR] objFile = objFSO.OpenTextFile(“[COLOR=”gray”]C:ScriptsTest.txt[/COLOR]”, ForWriting)
    objFile.WriteLine strNewText1
    objFile.Close

    strText2 = objFile.ReadAll
    objFile.Close
    strNewText2 = Replace(strText2, “[COLOR=”gray”]server.location2″, “server[/COLOR]”)

    [COLOR=”blue”]Set[/COLOR] objFile = objFSO.OpenTextFile(“[COLOR=”gray”]C:ScriptsTest.txt[/COLOR]”, ForWriting)
    objFile.WriteLine strNewText2
    objFile.Close

    CaTBuS
    CaTBuS
    Participant
    #309106

    Re: Find & Replace script – Expanded

    I figured out an answer to my own question. Here is what I have now:

    Code:
    [COLOR=”Blue”]Const[/COLOR] ForReading = [COLOR=”Red”]1[/COLOR]
    [COLOR=”blue”]Const[/COLOR] ForWriting = [COLOR=”red”]2[/COLOR]

    [COLOR=”blue”]Set[/COLOR] objFSO = CreateObject(“[COLOR=”Gray”]Scripting.FileSystemObject[/COLOR]”)
    [COLOR=”blue”]Set[/COLOR] objFile = objFSO.OpenTextFile(“[COLOR=”gray”]C:ScriptsTest.txt[/COLOR]”, ForReading)

    strText = objFile.ReadAll
    objFile.Close
    strNewText1 = Replace(strText, “[COLOR=”gray”]server1.location1″, “server1[/COLOR]”)
    strNewText2 = Replace(strNewText1, “[COLOR=”gray”]server2.location2″, “server2[/COLOR]”)

    [COLOR=”blue”]Set[/COLOR] objFile = objFSO.OpenTextFile(“[COLOR=”gray”]C:ScriptsTest.txt[/COLOR]”, ForWriting)
    objFile.WriteLine strNewText2
    objFile.Close

    Depending on the OS version (XP or Win7), this file will be located in a different location. What I need to do now is check the OS version, if XP, execute above code for one location, If Win7, execute code for other location. I found the below code to check OS. I now need to figure out how to combine the two:

    Code:
    strComputer = “.”

    Set objWMIService = GetObject(“winmgmts:\” & strComputer & “rootcimv2”)
    Set colOperatingSystems = objWMIService.ExecQuery (“Select * from Win32_OperatingSystem”)

    For Each objOperatingSystem in colOperatingSystems

    msg = objOperatingSystem.Caption & ” ” & objOperatingSystem.Version
    msgbox msg
    Next

    CaTBuS
    CaTBuS
    Participant
    #309107

    Re: Find & Replace script – Expanded

    All right!! I got the whole thing working nicely for anybody who is interested. This vbscript will determin the OS then perform a Find/Replace on an OS specific file. This will be implimented for our SAPLogon.ini file which is in different locations depending on which OS you are using. Anyway, here is the code:

    Code:
    [COLOR=”Blue”]On Error Resume Next[/COLOR]

    [COLOR=”blue”]Const[/COLOR] ForReading = [COLOR=”Red”]1[/COLOR]
    [COLOR=”blue”]Const[/COLOR] ForWriting = [COLOR=”red”]2[/COLOR]

    [COLOR=”blue”]Set[/COLOR] objFSO = CreateObject(“[COLOR=”Gray”]Scripting.FileSystemObject[/COLOR]”)
    [COLOR=”blue”]Set[/COLOR] objFileXP = objFSO.OpenTextFile(“[COLOR=”gray”]C:ScriptsTestXP.txt[/COLOR]”, ForReading)
    [COLOR=”blue”]Set[/COLOR] objFile7 = objFSO.OpenTextFile(“[COLOR=”gray”]C:ScriptsTest7.txt[/COLOR]”, ForReading)

    strComputer = “.”
    [COLOR=”blue”]Set[/COLOR] objWMIService = GetObject(“[COLOR=”gray”]winmgmts:\[/COLOR]” & strComputer & “[COLOR=”gray”]rootcimv2[/COLOR]”)

    [COLOR=”blue”]Set[/COLOR] colOperatingSystems = objWMIService.ExecQuery _
    (“[COLOR=”gray”]Select * from Win32_OperatingSystem[/COLOR]”)

    [COLOR=”blue”]For Each[/COLOR] objOperatingSystem [COLOR=”blue”]in[/COLOR] colOperatingSystems

    msg = objOperatingSystem.Caption & ” ” & _
    objOperatingSystem.Version

    [COLOR=”blue”]If[/COLOR] InStr(msg, “[COLOR=”gray”]XP[/COLOR]”) [COLOR=”blue”]Then[/COLOR]
    strText = objFileXP.ReadAll
    objFileXP.Close
    strText = Replace(strText, “[COLOR=”gray”]XXX[/COLOR]”, “[COLOR=”gray”]Windows XP[/COLOR]”)

    [COLOR=”blue”]Set[/COLOR] objFileXP = objFSO.OpenTextFile(“[COLOR=”gray”]C:ScriptsTestXP.txt[/COLOR]”, ForWriting)
    objFileXP.WriteLine strText
    objFileXP.Close
    [COLOR=”blue”]ElseIf[/COLOR] InStr(msg, “[COLOR=”gray”]Windows 7[/COLOR]”) [COLOR=”blue”]Then[/COLOR]
    strText = objFile7.ReadAll
    objFile7.Close
    strText = Replace(strText, “[COLOR=”gray”]XXX[/COLOR]”, “[COLOR=”gray”]Windows 7[/COLOR]”)

    [COLOR=”blue”]Set[/COLOR] objFile7 = objFSO.OpenTextFile(“[COLOR=”gray”]C:ScriptsTest7.txt[/COLOR]”, ForWriting)
    objFile7.WriteLine strText
    objFile7.Close
    [COLOR=”blue”]End If[/COLOR]

    [COLOR=”blue”]Next[/COLOR]

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

Register for this Petri Webinar!

Want to Make Your Backup Storage Unlimited & Ready for the Cloud? – Free Thurrott Premium Account with Webinar Registration!

Tuesday, August 27, 2019 @ 1:00 pm EDT

A Scale-Out Backup storage infrastructure is a must-have technology for your backups. In this webinar, join expert Rick Vanover for a look on what real-world problems are solved by the Scale-Out Backup Repository.

Register Now

Sponsored By