从数组中逐一读取 - VBScript

时间:2017-08-31 14:46:35

标签: arrays vbscript

我正在尝试从文件中读取,创建一个数组,然后逐行(逐个元素)逐行写入第二个文件,同时添加一些其他信息。下面的脚本将从文件中正确读取,但只会写入初始文件中的最后一行。我怎样才能一次从数组中写一个? (我可以用不同的方式将内容写入第二个文件,但我想直接从数组中完成。)

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(input, 
ForReading)

Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    arrFields = Split(strLine, ",")
    If InStr(arrFields(1), "False") Then
        strContents = strContents & arrFields(0) & vbCrlf
    End If
Loop

objFile.Close

Set objFile = objFSO.CreateTextFile(output)

' below correctly outputs contents
' objFile.Write strContents 

For each i in arrFields
    objFile.Write arrFields(0)
    objFile.Write "Number" & vbCrlf
Next

objFile.Close

我的测试csv文件如下所示从记事本中保存为“test.csv”。

1,FALSE
2,FALSE
3,FALSE
4,FALSE
5,FALSE
6,FALSE
7,FALSE
8,FALSE
9,FALSE
10,FALSE
11,FALSE
12,FALSE
13,TRUE

1 个答案:

答案 0 :(得分:0)

示例代码

Const ForReading = 1, ForWriting = 2, ForAppending = 3

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\pankaj.jaju\Desktop\test.csv", ForReading)

arrFields = Split(objFile.ReadAll, vbCrLf)
objFile.Close

Set objFile = objFSO.CreateTextFile("C:\Users\pankaj.jaju\Desktop\test1.txt")
For i = LBound(arrFields) To UBound(arrFields)
    objFile.Write "Number " & i+1 & "," & Split(arrFields(i),",")(0) & vbCrlf
Next
objFile.Close