1. ホーム
  2. スクリプト・コラム
  3. 腹筋

設定ファイル項目読み込み用VBS実装コード

2022-01-08 10:55:11

以下は、設定ファイルを読み込む関数です。
この関数は、以下の形式(.ini, .txt, .inf)の設定ファイルに対してのみ機能します。

[マーク1]です。
key1=キー1値
キー2=キー2バリュー
........
[マーク2]
key1=キー1値
キー2=キー2バリュー

コア・コード

'************************************************************
'Function: Reads the value of a configuration file (.ini, .txt format) configuration item and returns the value
' Parameters: FilePath - the full path to the configuration file
' Mark - configuration start marker
' Key - the name of the configuration item to be retrieved
' Call method: Ret = GetConfig("d:\configure.ini","Computer","IP")
' Author:Tiger Xiao Supreme
'Date:2013-06-20
'************************************************************
Function GetConfig(FilePath,Mark,Key)
 Dim fso, Str_ReadLine
 Set fso = CreateObject("Scripting.FileSystemObject")
 'Determine if the configuration file exists
 If fso.FileExists(FilePath) Then
 'Initialize the configuration flag, default is not found
 Flag = 0
 'Open the configuration file
 Set ConfigFile = fso.opentextfile(FilePath, 1)
 'Loop through the file data lines
 Do
  Str_ReadLine = ConfigFile.
  WScript.Echo Str_ReadLine
  'Determine if the read line is empty
  If Str_ReadLine <> "" Then
  'Determine if the read line is the start marker of the configuration to be found
  If LCase(Trim(Str_ReadLine))="[" & Lcase(Mark) & "]" Then
   'Found configuration start flag
   Flag = 1  
   'Loop through the configuration items under the current configuration start flag until the required configuration item is found under the current configuration flag
   ' or exit when the next configuration start flag appears
   Do
   Str_ReadLine = ConfigFile.
   retNum = InStr(Str_ReadLine,"=")
   'Check if the configuration item read has an equal sign
   If retNum > 0 Then
    'Determine if the name of the configuration item 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))
    'Exit Function when found
    Exit Function 
    End If
   End If
   'Determine if the current configuration item is the next start marker
   If (InStr(Str_ReadLine,"[")>0 And InStr(Str_ReadLine,"]")>0) Then
    'Flag the current configuration item to start flagging it for 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.
 'Close the file
 ConfigFile.Close
 Set fso = Nothing
 Else
 'The file is not found, give a message
 MsgBox "Configuration file "&"[" & FilePath &"] does not exist, please check if the path is correct. "
 End If
End Function

のインスタンスです。

d: \config.ini ファイルの [Computer2] の IP エントリーの値を読み込む必要があり、その内容は次のとおりです。

[コンピュータ1】の場合]
コンピュータ名=Computer1
IP=192.168.1.1
[コンピュータ2】の場合]
コンピュータ名=Computer2
IP=192.168.1.2

上記の関数を使用し

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

さて、ここで終了です。