Monday, October 30, 2017

To fetch the value of a variable from the text file.

Function fnGet_Value_From_TextFile(strFilePath,varName)
'function to fetch value of the variable from text file
On Error Resume Next 'Error Handling
'Constants for Reading and Tristate Use Default
Const ForReading = 1,ForWriting= 2,ForAppending = 8
'Path of the file to be modified
'strFilePath = Environment("Env_TestData_TextFile")
'Create FSO Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FileExists(strFilePath) Then
Call DoneResult("Specified file: '" & strFilePath & "' does not exist", "Not able to fetch the data from specified text file")
fnGet_Value_From_TextFile = ""
Exit Function
End If
'Get the file to be read.
Set objGetFile = objFSO.GetFile(strFilePath)
'This code will read the file as Text Stream
Set objTextFile = objGetFile.OpenAsTextStream(ForReading,-2)
'This code will read all the lines in the Config file
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
var_found = Instr(1,strNextLine,varName,1)
Flag = 0
If var_found> 0 Then
Flag = 1
'get the value of the  specified variable
var_SubString = Mid(strNextLine,var_found+Len(varName)+3)
fnGet_Value_From_TextFile = var_SubString
Call DoneResult("Value: " & varValue & " is retrieved for the variable: " & varName & " in the text file: " & strFilePath, varName & " = " & varValue)
Exit Do
End If
Loop
'This code will close the file after reading
objTextFile.Close
Set objTextFile = Nothing
Set objGetFile = Nothing
Set objFSO = Nothing
If Flag = 0 Then 'if the variable does not exist in the File, returns empty
fnGet_Value_From_TextFile = ""
Exit Function
End If
If Err.Number <> 0  Then
Call DoneResult("Exception error " & Err.Number & " with the description " & Err.Description & " occured while retrieving the value from Text file", "Data Retreival is  failed")
fnGet_Value_From_TextFile = ""
End If
End Function

No comments:

Post a Comment