Automatically and quickly replacing text in few files

Preface

In some cases it is necessary to replace text in some text files. Unfortunately this is a really inconvenient work because we need always copy the proper text to the clipboard then we need to switch the window then highlighting the old text part and then pasting the text from the clipboard. If we have only one text file where the text is need to be replaced then it is fine but if we have more text files then one that is a really annoying something.

Solution

Fortunately VBS language is able to help us. I know the VBS is a bit old school but sometimes we do not have opportunity having PowerShell. Of course it is possible installing PowerShell to Windows Server 2003 but like I said sometimes it is simple not allowed to do it. So VBS is the forever alternative.

The script

I also started my script with a specific VBS instruction: on error resume next

' option explicit
on error resume next

This line helps us to continue the running of the script if any error occurs.[1] I know this is not the best solution but in this case we can use it effectively.

Next two lines define constants.

Const ForReading = 1
Const ForWriting = 2

They are necessary because the same function will be used for Reading and Writing as well. Therefore we need to ensure that the variables hold the same value during the running of the script.

My script uses arguments which provide information and the script will act accordingly. The first argument is a “switch” that helps to make a decision in an “if” case.

sLocation = WScript.Arguments(0)


sLocation = WScript.Arguments(0)
If UCase(sLocation) = "LOCATION1" Then
sAddress = "192.168.1.1"
ElseIf UCase(sLocation) = "LOCATION2" Then
sAddress = "192.168.1.2"
End If

Since we can see if the first argument is Location1 the variable will be set accordingly but if the argument is Location2 the variable will be something else. There is “Ucase” built-in VBS function because the argument can be provided in small capitals as well and we need to ensure the equality of the two strings.
x86 or x64

The next line is very very useful. There are so many cases when we need to know the architecture of the system. In our case it is important because we need to modify files according to the type of the architecture. First of all here is this “magic” command:

sBits = GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth


If sBits = 64 Then
sFiles = Array("C:\Folderx64\File1","C:\Folderx64\File2","C:\Folderx64\File3")
ElseIf sBits = 32 Then
sFiles = Array("C:\Folder\File1","C:\Folder\File2","C:\Folder\File3")
End If

sBits variable contains the useful value that helps to decide which file should be used (in which should be replaced the text).

One more interesting thing is here. I put the files into an array and the content of the array depends on the architecture of the system.

More arguments

In my script I do not use only one argument. Truthfully it depends on the number of those “strings” that should be replaced. It can be few but sometimes a lot.

sCMO_NAME = WScript.Arguments(1)
sOLD_SID = WScript.Arguments(2)
sNEW_SID = WScript.Arguments(3)


sOLD_CT_CMSLIST = WScript.Arguments(4)


sOLD_vNAME = WScript.Arguments(5)


sTLMAGENT_INI_OLDSERVERIP = WScript.Arguments(6)

So like I mentioned these arguments are the base of the replacement.

The soul of the script

First of all let’s see the code than we analyse it

iCounter = 0
For Each item In sTivoliFiles
iCounter = iCounter + 1
If iCounter = 10 Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(item, ForWriting)
objFile.Write ""
sSystemInfoContent="system_id=" & sNEW_SID & vbNewLine & "container=" & sCMO_NAME & vbNewLine & "mon=itm6"
objFile.Write sSystemInfoContent
objFile.Close
Else
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(item, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, sOLD_CT_CMSLIST, sNEW_CT_CMSLIST)
strNewText = Replace(strNewText, sOLD_CT_CMSLIST_NO_HASHMARK, sNEW_CT_CMSLIST)
strNewText = Replace(strNewText, sOLD_CTIRA_HOSTNAME, sNEW_CTIRA_HOSTNAME)
strNewText = Replace(strNewText, sOLD_PH_CTIRA_SYSTEM_NAME, sNEW_PH_CITRA_SYSTEM_NAME)
strNewText = Replace(strNewText, sIPV4_KDC_FAMILIES, sIPV6_KDC_FAMILIES)
strNewText = Replace(strNewText, sIPV4_KDC_FAMILIES_SMALL_CAPS, sIPV6_KDC_FAMILIES)
strNewText = Replace(strNewText, sOLD_CTIRA_SYSTEM_NAME, sNEW_CITRA_SYSTEM_NAME)
strNewText = Replace(strNewText, sTLMAGEN_INI_OLDSID, sTLMAGEN_INI_NEWSID)
strNewText = Replace(strNewText, sTLMAGEN_INI_OLDvNAME, sTLMAGEN_INI_NEWvNAME)
strNewText = Replace(strNewText, sTLMAGEN_INI_SERVER, sTLMAGEN_INI_NEWSERVERIP)
strNewText = Replace(strNewText, sOLD_CTIRA_SYSTEM_NAME_w_OLDvNAME, sNEW_CTIRA_SYSTEM_NAME_w_NEWvNAME)
Set objFile = objFSO.OpenTextFile(item, ForWriting)
objFile.WriteLine strNewText
objFile.Close
End If
Next

I use the “for .. each” looping because this is a very effective way to enumerate an array.

I need a co-variable during the run because I would like to have information about the number of the cycles. I know that I have eleven files in my array (yes the screenshot above is about a different example) therefore if the counter reaches the 10th I do something else with that specific file.

In the “else” junction I replace the texts according to the original plans.

  1. I create an object (set objFSO) from the “Scripting.FileSystemObject” class.
  2. I create a different object (set objFile) from that file in which I need to replace something (objFSO.OpenTextFile(“”, ForReading) – remember our constant variable.
  3. I read all from the file to a variable
  4. and I close the object (objFile.Close)
  5. I use the Replace built-in VBS function which has three arguments: whole content of the old file, the old text and the new text – and this will be stored in a variable
  6. I repeat this until it is necessary – it depends on the number of the replacement.
  7. Finally I open again the file in which the whole replacement should be done [objFile.OpenTextFile(“”, ForWriting)]
  8. the WriteLine method rewrite the whole file with the new text(s)
  9. Close command close and save the file down.

Summary

This script maybe not the most perfect solution for the problem but if we need to replace something in some files this script can server perfectly.

[1] When an error occurs, the execution will continue on the next line without interrupting the script. (http://stackoverflow.com/questions/2202869/what-does-the-on-error-resume-next-statement-do)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s