code: VBScript
description: VBScriptでINIファイルの読み書きをできるようにする
Last active
August 11, 2021 02:41
-
-
Save eggist77/e56061cea9d594d752341a237d1c565e to your computer and use it in GitHub Desktop.
vbs: get_ini_lib.vbs
iniファイルの値を取得するVBScript
パラメータのグループ分けに使われる。セクションは必ず付ける必要がある。
[section]
name=value
コメント開始はセミコロン(;)
; comment
- 大文字と小文字の区別:区別あり
- 空行:許可
- 名前/値の区切り文字:=(イコール)のみ
- 階層構造:無し
- 行頭にあるスペース:無視する
- セクションが宣言されていないパラメータは無視する
readINI:iniファイルの情報を連想配列に入れる。getINIに呼び出されている内部的な関数
getINI:連想配列からセクションとキーに該当する値を取り出す
sectionName:セクション名
keyName:キー名
fileName:INIファイル名
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function get_ini(sectionName,keyName,iniFile) | |
Set iniDic = read_ini(iniFile) | |
get_ini = iniDic.Item(sectionName).Item(keyName) | |
End Function | |
Function read_ini(iniFile) | |
Dim fso 'file system object | |
Set fso = CreateObject("Scripting.FileSystemObject") | |
If fso.FileExists(iniFile) Then | |
Dim dic 'Dictionary | |
Set dic = CreateObject("Scripting.Dictionary") | |
Dim sectionDic 'section Dictionary | |
Set sectionDic = CreateObject("Scripting.Dictionary") | |
Dim line | |
Dim sectionName : sectionName = "" | |
Dim a 'array | |
Dim f 'file | |
Set f = fso.OpenTextFile(iniFile) | |
Do Until f.AtEndOfStream | |
line = Trim(f.ReadLine) | |
'Section | |
If Left(line,1) = "[" And Right(line,1) = "]" Then | |
sectionName = Mid(line, 2, Len(line) - 2) | |
If Not dic.Exists(sectionName) Then | |
dic.Add sectionName, sectionDic | |
End If | |
'Parameter | |
ElseIf Instr(line,"=") > 1 And sectionName <> "" Then | |
'Key & Value | |
a = Split(line,"=") | |
dic(sectionName).Add Trim(a(0)), Trim(a(1)) | |
'comment' | |
ElseIf Left(line,1) = ";" Then | |
End If | |
Loop | |
Set read_ini = dic | |
f.Close | |
Set f = Nothing | |
Set sectionDic = Nothing | |
Set dic = Nothing | |
End If | |
Set fso = Nothing | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment