Reading text circularly with vbs

Time:2021-10-12

Because a batch of URL data needs to be read for browsing during the test, in fact, multiple reading methods can be realized by using QTP’s own table. However, because the tab needs to be saved in Excel, running this part of the script on a machine without Excel or vsita will encounter problems. In order to avoid unnecessary trouble, TXT is used to save the website data.

However, it seems that VBS does not provide a function to set the file reading location (provided by Python). This available code can not be found on the Internet. Maybe you don’t have my low-level requirements. Embarrassed RZ… Later, it was done, so the code was posted as a memo. In fact, the core is to find the last line of the read text, so reopen the file.

code:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
msgbox(GetIni("d://vbscript//config.txt"))
Function GetIni(strIniFilePath )
 Const ForReading = 1
 Const TriStateTrue = -2
 Dim myFso
 Dim MyFile
 Set myFso = CreateObject("Scripting.FileSystemObject")
 Set MyFile = myFso.OpenTextFile(strIniFilePath,ForReading,False,TriStateTrue)
   GetIni = MyFile.ReadLine()
   If MyFile.AtEndOfStream=True Then
        Set MyFile = Nothing
        Set MyFile = myFso.OpenTextFile(strIniFilePath,ForReading,False,TriStateTrue)
   End If
 MyFile.Close
 Set MyFile = Nothing
 Set myFso = Nothing
End Function

config.txt

http://www.zzvips.com

The above code is relatively simple, and only the data in the first line can be obtained. It is recommended that you use the following code to read the configuration file

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'************************************************************
'function: read the value of the configuration item in the configuration file (. Ini,. TXT format) and return the value
'parameter: filepath - the full path of the configuration file
'mark - configure start tag
'key - the name of the configuration item to be obtained
'call method: RET = getconfig ("D: \ configure. Ini", "computer", "IP")
Author: Huxiao supreme
'date: June 20, 2013
'************************************************************
Function GetConfig(FilePath,Mark,Key)
 Dim fso, Str_ReadLine
 Set fso = CreateObject("Scripting.FileSystemObject")
 'judge whether the configuration file exists
 If fso.FileExists(FilePath) Then
 'initialize configuration flag, default to not found
 Flag = 0
 'open profile
 Set ConfigFile = fso.opentextfile(FilePath, 1)
 'loop reading file data lines
 Do
 Str_ReadLine = ConfigFile.ReadLine
 WScript.Echo Str_ReadLine
 'judge whether the read data line is empty
 If Str_ReadLine <> "" Then
 'judge whether the read data line is the configuration start flag to be searched
 If LCase(Trim(Str_ReadLine))="[" & Lcase(Mark) & "]" Then
 'find configuration start tag
 Flag = 1
 'cycle through the configuration items under the current configuration start tag until the required configuration item is found under the current configuration tag
 'or exit when the next configuration item start flag appears
 Do
 Str_ReadLine = ConfigFile.ReadLine
 retNum = InStr(Str_ReadLine,"=")
 'check whether the read configuration item has an equal sign
 If retNum > 0 Then
 'judge whether the obtained 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 equal sign of the configuration item
 GetConfig = Trim(Right(Str_ReadLine,Len(Str_ReadLine)-retNum))
 'when found, exit the function
 Exit Function
 End If
 End If
 'judge whether the current is the start mark of the next configuration item
 If (InStr(Str_ReadLine,"[")>0 And InStr(Str_ReadLine,"]")>0) Then
 'mark the beginning of the current configuration item as the next configuration
 Flag = 0
 'exit function
 Exit Function
 End If
 Loop Until (Flag = 0 Or ConfigFile.AtEndOfStream)
 End If
 End If
 Loop Until ConfigFile.AtEndOfStream
 'close file
 ConfigFile.Close
 Set fso = Nothing
 Else
 'if the file is not found, a prompt message will be given
 MsgBox "Profile"&"[" & FilePath &"] does not exist, please check whether the path is correct."
 End If
End Function

example:

We need to read the value of the IP entry under [computeR2] in the D: \ config \ environment.ini file. 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 function to get

IP = GetConfig(“d:\config\environment.ini”,”Computer2″,”IP”)
Msgbox IP

All right, that’s it

Recommended Today

Leetcode questions 13 and 14 — Roman numerals to integers, longest common prefix

1. Leetcode question 13Requirements: https://leetcode-cn.com/probl… Important note: usually, the small Roman numerals are to the right of the large ones. But there are also special cases. For example, 4 is not written as IIII, but IV. The number 1 is on the left of the number 5, and the number represented is equal to the […]