How to Read and Write INI Files with VBScript
Sometimes we need to store some values in a file during a UFT test run. In UFT test script, we can store variables in an .ini file. Let’s see how to read and write ini files with VBScript during test run. If you are familiar with .properties file, you can think of it as somewhat similar. Storing values in ini file is such a popular way. Once we store any value in ini file, or even if it is saved in the file before test run, We an read the values during test run. It is a very light weight and doesn’t use much memory. Use of excel to store 2 or 3 values seems lengthy or useless.
Let’s see how to read and write ini files with vbscript code examples.
1. Example code to read and write ini files with vbscript– Store variables in .ini file:
Public Function setGlobal (par_name, par_value)
' Pupose: Store parameters variable
gDATAFOLDER= Environment.Value("TestLocation or some path") &"Data"
gPARFILENAME = gDATAFOLDER & "qtpparam.ini"
Dim rc
Extern.Declare micInteger,"WritePrivateProfileStringA", "kernel32.dll","WritePrivateProfileStringA",micString, micString, micString, micString
rc = Extern.WritePrivateProfileStringA("PARAMS",par_name,par_value, gPARFILENAME )
FnReadGlobalFile
End function
2. Example code to read and write ini files with vbscript : Read variables stored in .ini file:
Public Function FnReadGlobalFile
' Pupose: To Reterive the data's from data sheet dyanmically
Dim gDATAFOLDER
Dim gPARFILENAME
gDATAFOLDER= Environment.Value("TestLocation or some path") &"Data"
gPARFILENAME = gDATAFOLDER & "qtpparam.ini"
Dim fArrFileLines()
i = 0
Set fObjFSO = CreateObject("Scripting.FileSystemObject")
If (fObjFSO.FolderExists(gDATAFOLDER) = False) Then
fObjFSO.CreateFolder(gDATAFOLDER)
End If
If fObjFSO.FileExists(gPARFILENAME)=False Then
Exit Function
End If
Set fObjFile = fObjFSO.OpenTextFile(gPARFILENAME, 1)
Do Until fObjFile.AtEndOfStream
Redim Preserve fArrFileLines(i)
fArrFileLines(i) = fObjFile.ReadLine
i = i + 1
Loop
fObjFile.Close
For l = Ubound(fArrFileLines) to 1 Step -1
fTempstr=split (fArrFileLines(l),"=")
fvar= fTempstr(0)
fval=fTempstr(1)
Execute fVar & "=fVal"
Next
Set fObjFSO=Nothing
End Function
Discover more from Automation Script
Subscribe to get the latest posts sent to your email.