Skip to main content

QTP Script to update variables value from ini file

'Subprocedure to update value of ini variables value
Public Sub WriteToEnvironment(sParam ,sValue)
'Declare the required variables
Dim oFSO, oFile, oTxtStream, sLine, sNewTextStream, vpath

'Create the application object for FileSystemObject
Set oFSO = CreateObject("Scripting.FileSystemObject")

'Declare the file input/output constants
Const ForReading = 1, ForWriting = 2, ForAppending = 8

'Declare Tristate values constant used to indicate the format of the opened file
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

'Get the file from the file specified in environment variable 
Set oFile = oFSO.GetFile(Environment("iniPath"))

'Open the ini file for reading purpose
Set oTxtStream = oFile.OpenAsTextStream(ForReading, TristateUseDefault)

'Read the file till the end
While Not oTxtStream.AtEndOfStream

'Read the file line by line
sLine = oTxtStream.ReadLine
sParameter = Array()

'Spilt the ini variable by delimiter
sParameter = Split(sLine, "=")

'If the ini variable name matches to required then modify the value with new value
If sParameter(0) = sParam Then
sLine = sParam & "=" & sValue
End If

sNewTextStream = sNewTextStream & sLine & vbCrLf
Wend

'close the stream object
oTxtStream.Close

'Create the application object for FileSystemObject
Set oFSO = CreateObject("Scripting.FileSystemObject")

'Get the file from the file specified in environment variable 
Set oFile = oFSO.GetFile(Environment("iniPath"))

'Open the ini file for writhing purpose
Set oTxtStream = oFile.OpenAsTextStream(ForWriting, TristateUseDefault)

'Write the new stream data to file
oTxtStream.Write sNewTextStream

'Close the stream object
oTxtStream.Close

End Sub

Comments