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))="[" & Lcase(Mark) & "]" 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,"[")>0 And InStr(Str_ReadLine,"]")>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"&"[" & FilePath &"]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.