gpt4 book ai didi

vbscript - 从ini文件中读取数据

转载 作者:行者123 更新时间:2023-12-02 22:10:06 25 4
gpt4 key购买 nike

我有一个需要输入文件名的 vbscript

代码是

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

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

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

这是我的ini文件

//我的ini文件

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

[col]
Number1=4
Number2=5

最佳答案

.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

输出:

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

(请参阅this answer了解背景)

更新评论/编辑:

我将您的部分添加到我的示例 .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

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

  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 部分”会导致任何问题。

关于vbscript - 从ini文件中读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21825192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com