How to put a 2 array in config (.ini) file? [dupli

2019-03-07 07:56发布

问题:

This question already has an answer here:

  • Reading and writing an INI file 1 answer

I have this simple basic code of VBScript.

Dim cars: cars = Array("Volvo", "Saab", "BMW")
Dim fruits: fruits = Array("Apple", "Orange", "Banana")

Dim i: i = 0

For i = 0 To UBound(cars)
  Call Response.Write(cars(i) & " " & fruits(i))
Next

Output:

Volvo Apple
Saab Orange
BMW Banana

I want to put all the variables in a config .ini file in a way that the array variable is still in match. (e.g. Volvo and Apple for Volvo Apple) Anyone know or have an idea to do it?

I tried to search for this on the internet but no topic for this one. A big appreciation for the answer.

回答1:

Instead of using an array use a disconnected recordset. They can be saved to file in binary or xml format.

This stores lines in a file then sorts the recordset on line number and writes it back.

set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
Dim LineCount
Set rs = CreateObject("ADODB.Recordset")
With rs
    .Fields.Append "LineNumber", 4 
    .Fields.Append "Txt", 201, 5000 
    .Open
    LineCount = 0
    Do Until Inp.AtEndOfStream
        LineCount = LineCount + 1
        .AddNew
        .Fields("LineNumber").value = LineCount
        .Fields("Txt").value = Inp.readline
        .UpDate
    Loop
    .Sort = "LineNumber DESC"
    Do While not .EOF
        Outp.writeline .Fields("Txt").Value
        .MoveNext
    Loop
End With

To use

cscript //nologo file.vbs < %windir%\win.ini

And from Help for a recordset

Save Method Saves the Recordset in a file or Stream object.

Syntax

 recordset.Save Destination, PersistFormat 

Parameters

Destination Optional. A Variant that represents the complete path name of the file where the Recordset is to be saved, or a reference to a Stream object.

PersistFormat Optional. A PersistFormatEnum value that specifies the format in which the Recordset is to be saved (XML or ADTG). The default value is adPersistADTG. Remarks