从ini文件中读取数据

从ini文件中读取数据

本文介绍了从ini文件中读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用输入文件名的vbscript

i have a vbscript that takes input file name

代码是

 Dim tsout: Set tsout = gofs.CreateTextFile("C:\....csv")
 Dim tsin: Set tsin = gofs.OpenTextFile("C:\.....csv")

我该如何配置它,以便从配置文件(.ini)中读取路径createTextFile(....)

how can i configure this so that path createTextFile(....) is read from config file(.ini)

用于创建和写入输出到的文件路径,必须取自ini文件

the file path for creating and writing output-to, must be taken from ini file

这是我的ini文件

//我的ini文件

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

    [col]
Number1=4
Number2=5

推荐答案

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

A simple version of an .ini file parser:

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

输出:

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

(有关背景情况,请参见此答案)

(see this answer for background)

更新评论/

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

I added your sections to my sample .ini file:

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

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

and - just for clarity - changed the final output line of my demoReadIniFile() function to:

  WScript.Echo "tsout.Path", dicIni("tsout")("Path")
  WScript.Echo "Read_file.tsin", dicIni("Read_file")("tsin")
  WScript.Echo "col.Number2", dicIni("col")("Number2")

输出:

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

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

So I don't understand at all why accessing 'the col section taking out number1=4 and Number2=5' causes any problems.

这篇关于从ini文件中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 01:04