SoFunction
Updated on 2025-04-08

Use vbs to implement text loop reading

Because a batch of URL data is required to be read in the test, in fact, using QTP's own table can achieve multiple reading methods. However, because the tab is required to be saved using Excel, running this part of the script on a machine without Excel or a vsita machine will encounter problems. For unnecessary trouble, txt is used to save the URL data.

However, vbs does not seem to provide a function to set the file reading location (provided by python). This available code is not found on the Internet. Maybe everyone does not have as low-level requirements as me.... Later, I did it, so I posted the code to make a memo. In fact, the core is to find that the last line of the text is read, so just open the file again.

Code:

msgbox(GetIni("d://vbscript//"))
Function GetIni(strIniFilePath ) 
 Const ForReading = 1
 Const TriStateTrue = -2
 Dim myFso 
 Dim MyFile 
 Set myFso = CreateObject("")
 Set MyFile = (strIniFilePath,ForReading,False,TriStateTrue)
   GetIni = ()
   If =True Then 
   		Set MyFile = Nothing 
   		Set MyFile = (strIniFilePath,ForReading,False,TriStateTrue)
   End If 
 
 Set MyFile = Nothing
 Set myFso = Nothing
End Function

https://

The above code is relatively simple, and can only obtain the first line of data. It is recommended that you use the following code to implement configuration file reading.

The following is a function that reads the configuration file:
This function is only applicable to configuration files (.ini,.txt,.inf) in the following formats:

[Mark1]
key1=key1value
key2=key2value
........
[Mark2]
key1=key1value
key2=key2value

Core code

'************************************************************
'Function:Read configuration files(.ini,.txtFormat)The value of the configuration item,and return the value
'Parameter:FilePath - Full path to the configuration file
 ' Mark - Configuration start marker
' Key - The configuration item name that needs to be obtained
 'Calling methods:Ret = GetConfig("d:\","Computer","IP")
'Author: Tiger's Supreme
 'date:2013-06-20
'************************************************************
Function GetConfig(FilePath,Mark,Key)
 Dim fso, Str_ReadLine
 Set fso = CreateObject("")
 'Determine whether the configuration file exists
 If (FilePath) Then
 'Initialize configuration tags,Default is not found
 Flag = 0
 'Open the configuration file
 Set ConfigFile = (FilePath, 1)
 'Looping the file data lines
 Do
 Str_ReadLine = 
  Str_ReadLine
 'Just determine whether the read data line is empty
  If Str_ReadLine <> "" Then
  '判断读取数据行是否为需要查找的Configuration start marker
 If LCase(Trim(Str_ReadLine))="[" &amp; Lcase(Mark) &amp; "]" Then
 '找到Configuration start marker
 Flag = 1 
 'Read the configuration item under the current configuration start mark until the required configuration item is found under the current configuration mark
  'or exit when the next configuration item start mark appears
 Do
 Str_ReadLine = 
 retNum = InStr(Str_ReadLine,"=")
 'Check whether the read configuration item has an equal sign
  If retNum > 0 Then
  'Determine whether the configuration item name is the required configuration item
 If Trim(LCase(Left(Str_ReadLine,retNum-1)))= Trim(LCase(Key)) Then
 'Get the data after the configuration item equal sign
 GetConfig = Trim(Right(Str_ReadLine,Len(Str_ReadLine)-retNum))
 'After finding, exit the function
  Exit Function
  End If
  End If
  'Determine whether the current configuration item start mark is currently the next configuration item
 If (InStr(Str_ReadLine,"[")&gt;0 And InStr(Str_ReadLine,"]")&gt;0) Then
 'Mark the current configuration item starts marked as next configuration
 Flag = 0
 'Exit function
 Exit Function
 End If
 Loop Until (Flag = 0 Or )
 End If
 End If 
 Loop Until 
 'Close the file
 
 Set fso = Nothing
 Else
 'file not found,Give prompt information
 MsgBox "Profile"&amp;"[" &amp; FilePath &amp;"]Not exists, please check if the path is correct."
 End If
End Function

Example:

We need to read the value of the IP item under [Computer2] of the d:\config\ file, and the file content is as follows:

[Computer1]
ComputerName=Computer1
IP=192.168.1.1
[Computer2]
ComputerName=Computer2
IP=192.168.1.2

Use the above functions to get

IP = GetConfig("d:\config\","Computer2","IP")
Msgbox IP

Well, it's done here.