Announcement

Collapse
No announcement yet.

Find & Replace script - Expanded

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Find & Replace script - Expanded

    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:
    Const ForReading = 1
    Const ForWriting = 2
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForReading)
    
    strText1 = objFile.ReadAll
    objFile.Close
    strNewText1 = Replace(strText1, "server.location1", "server")
    
    Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", 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:
    Const ForReading = 1
    Const ForWriting = 2
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForReading)
    
    strText1 = objFile.ReadAll
    objFile.Close
    strNewText1 = Replace(strText1, "server.location1", "server")
    
    Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForWriting)
    objFile.WriteLine strNewText1
    objFile.Close
    
    strText2 = objFile.ReadAll
    objFile.Close
    strNewText2 = Replace(strText2, "server.location2", "server")
    
    Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForWriting)
    objFile.WriteLine strNewText2
    objFile.Close

  • #2
    Re: Find & Replace script - Expanded

    I figured out an answer to my own question. Here is what I have now:
    Code:
    Const ForReading = 1
    Const ForWriting = 2
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForReading)
    
    strText = objFile.ReadAll
    objFile.Close
    strNewText1 = Replace(strText, "server1.location1", "server1")
    strNewText2 = Replace(strNewText1, "server2.location2", "server2")
    
    Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", 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 & "\root\cimv2")
    Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
    	
    For Each objOperatingSystem in colOperatingSystems
    
    msg = objOperatingSystem.Caption & " " & objOperatingSystem.Version
    msgbox msg
    Next
    Last edited by CaTBuS; 20th June 2011, 21:57. Reason: Update project scope

    Comment


    • #3
      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:
      On Error Resume Next
      
      Const ForReading = 1
      Const ForWriting = 2
      
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objFileXP = objFSO.OpenTextFile("C:\Scripts\TestXP.txt", ForReading)
      Set objFile7 = objFSO.OpenTextFile("C:\Scripts\Test7.txt", ForReading)
      
      strComputer = "."
      	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
      
       Set colOperatingSystems = objWMIService.ExecQuery _
      		("Select * from Win32_OperatingSystem")
      		
       For Each objOperatingSystem in colOperatingSystems
       
       msg = objOperatingSystem.Caption & " " & _
      		objOperatingSystem.Version
      
      If InStr(msg, "XP") Then
      	strText = objFileXP.ReadAll
      	objFileXP.Close
      	strText = Replace(strText, "XXX", "Windows XP")
      
      	Set objFileXP = objFSO.OpenTextFile("C:\Scripts\TestXP.txt", ForWriting)
      	objFileXP.WriteLine strText
      	objFileXP.Close
      ElseIf InStr(msg, "Windows 7") Then
      	strText = objFile7.ReadAll
      	objFile7.Close
      	strText = Replace(strText, "XXX", "Windows 7")
      
      	Set objFile7 = objFSO.OpenTextFile("C:\Scripts\Test7.txt", ForWriting)
      	objFile7.WriteLine strText
      	objFile7.Close
      End If
      
      Next

      Comment

      Working...
      X