从ini文件中读取数据

vha*_*lgi 3 vbscript ini

我有一个输入文件名的vbscript

代码是

 Dim tsout: Set tsout = gofs.CreateTextFile("C:\....csv")
 Dim tsin: Set tsin = gofs.OpenTextFile("C:\.....csv")
Run Code Online (Sandbox Code Playgroud)

如何配置这个以便createTextFile(....)从配置文件中读取路径(.ini)

用于创建和写入输出的文件路径必须从ini文件中获取

这是我的ini文件

//我的ini文件

[Read_file]
tsout=E:.....tt.csv
tsin=E:\....gt.csv

    [col]
Number1=4
Number2=5
Run Code Online (Sandbox Code Playgroud)

Ekk*_*ner 9

简单版本的.ini文件解析器:

Option Explicit

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

'WScript.Quit demoReadFile()
WScript.Quit demoReadIniFile()

Function demoReadFile()
  demoReadFile = 0
  Dim tsIn : Set tsIn = goFS.OpenTextFile(".\21825192.ini")
  Do Until tsIn.AtEndOfStream
     Dim sLine : sLine = tsIn.ReadLine()
     WScript.Echo tsIn.Line - 1, sLine
  Loop
  tsIn.Close
End Function

Function demoReadIniFile()
  demoReadIniFile = 0
  Dim dicIni : Set dicIni = ReadIniFile(".\21825192.ini")
  Dim sSec, sKV
  For Each sSec In dicIni.Keys()
      WScript.Echo "---", sSec
      For Each sKV In dicIni(sSec).Keys()
          WScript.Echo " ", sKV, "=>", dicIni(sSec)(sKV)
      Next
  Next
  WScript.Echo dicIni("tsout")("Path")
End Function

Function ReadIniFile(sFSpec)
  Dim dicTmp : Set dicTmp = CreateObject("Scripting.Dictionary")
  Dim tsIn   : Set tsIn   = goFS.OpenTextFile(sFSpec)
  Dim sLine, sSec, aKV
  Do Until tsIn.AtEndOfStream
     sLine = Trim(tsIn.ReadLine())
     If "[" = Left(sLine, 1) Then
        sSec = Mid(sLine, 2, Len(sLine) - 2)
        Set dicTmp(sSEc) = CreateObject("Scripting.Dictionary")
     Else
        If "" <> sLine Then
           aKV = Split(sLine, "=")
           If 1 = UBound(aKV) Then
              dicTmp(sSec)(Trim(aKV(0))) = Trim(aKV(1))
           End If
        End If
     End If
  Loop
  tsIn.Close
  Set ReadIniFile = dicTmp
End Function
Run Code Online (Sandbox Code Playgroud)

输出:

cscript 21825192.vbs
1 [pipapo]
2 Path=E:\dont\find\me.csv
3   Some  = thing else
4
5 [tsout]
6 Path=E:\where\ever\output.csv
7 abc=def

cscript 21825192.vbs
--- pipapo
  Path => E:\dont\find\me.csv
  Some => thing else
--- tsout
  Path => E:\where\ever\output.csv
  abc => def
E:\where\ever\output.csv
Run Code Online (Sandbox Code Playgroud)

(见背景答案)

更新wrt评论/编辑:

我将您的部分添加到我的示例.ini文件中:

type 21825192.ini
[pipapo]
Path=E:\dont\find\me.csv
  Some  = thing else

[tsout]
Path=E:\where\ever\output.csv
abc=def

[Read_file]
tsout=E:.....tt.csv
tsin=E:\....gt.csv

    [col]
Number1=4
Number2=5
Run Code Online (Sandbox Code Playgroud)

并且 - 为了清楚起见 - 将我的demoReadIniFile()函数的最终输出行更改为:

  WScript.Echo "tsout.Path", dicIni("tsout")("Path")
  WScript.Echo "Read_file.tsin", dicIni("Read_file")("tsin")
  WScript.Echo "col.Number2", dicIni("col")("Number2")
Run Code Online (Sandbox Code Playgroud)

输出:

cscript 21825192.vbs
--- pipapo
  Path => E:\dont\find\me.csv
  Some => thing else
--- tsout
  Path => E:\where\ever\output.csv
  abc => def
--- Read_file
  tsout => E:.....tt.csv
  tsin => E:\....gt.csv
--- col
  Number1 => 4
  Number2 => 5
tsout.Path E:\where\ever\output.csv
Read_file.tsin E:\....gt.csv
col.Number2 5
Run Code Online (Sandbox Code Playgroud)

所以我完全不明白为什么访问'col部分取出number1 = 4和Number2 = 5'会导致任何问题.