我有一个采用输入文件名
的vbscript代码是
Dim tsout: Set tsout = gofs.CreateTextFile("C:\....csv")
Dim tsin: Set tsin = gofs.OpenTextFile("C:\.....csv")
如何配置此功能以便从配置文件(createTextFile(....)
)中读取路径.ini
创建和写入output-to的文件路径必须取自ini
file
这是我的ini文件
//我的ini文件
[Read_file]
tsout=E:.....tt.csv
tsin=E:\....gt.csv
[col]
Number1=4
Number2=5
答案 0 :(得分: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
输出:
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
所以我完全不明白为什么访问'col部分取出number1 = 4和Number2 = 5'会导致任何问题。
答案 1 :(得分:0)
VBS不支持IniFile类。您需要创建自己的INI文件解析器。其他解决方案(也可以创建自己的解析函数,但比解析INI文件容易得多):保存TAB分隔数据,例如:首先提交输出文件,而不是TAB字符,而不是INcomming文件。
答案 2 :(得分:0)
Ekkehard为Classic ASP调整了演示:
Function demoReadIniFile()
demoReadIniFile = 0
Dim dicIni : Set dicIni = ReadIniFile("c:\path\to\21825192.ini")
Dim sSec, sKV
For Each sSec In dicIni.Keys()
response.write "---" & sSec & "<br>"
For Each sKV In dicIni(sSec).Keys()
response.write " " & sKV & " => " & dicIni(sSec)(sKV) & "<br>"
Next
Next
' response.write dicIni("tsout")("Path") & "<br>"
End Function
他的ReadIniFile()按照书面工作。