问题
i have a vbscript that takes input file name
the code is
Dim tsout: Set tsout = gofs.CreateTextFile("C:\....csv")
Dim tsin: Set tsin = gofs.OpenTextFile("C:\.....csv")
how can i configure this so that path createTextFile(....)
is read from config file(.ini
)
the file path for creating and writing output-to, must be taken from ini
file
this is my ini file
// my ini file
[Read_file]
tsout=E:.....tt.csv
tsin=E:\....gt.csv
[col]
Number1=4
Number2=5
回答1:
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
output:
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)
Update wrt comment/edit:
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
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")
The output:
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
So I don't understand at all why accessing 'the col section taking out number1=4 and Number2=5' causes any problems.
回答2:
VBS do not support IniFile class. You need to create own parser of INI file. Other solution (also with creating own parsing functions, but much easy than to parse INI file): save TAB delimited data, for example: first comming OUTgoing file, than TAB char, than INcomming file.
回答3:
Ekkehard's demo tweaked for 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
His ReadIniFile() works as written.
来源:https://stackoverflow.com/questions/21825192/read-data-from-ini-file