关于动态数组的困惑

时间:2015-10-14 05:37:31

标签: arrays vbscript

好的,所以我使用下面的代码来读取和显示文本文件到带有VBScript的HTA,它循环遍历文件夹中的所有文本文件(Notes)。我解析了返回并从显示中删除了文件扩展名。

我想要做的是构建2个数组,一个用于文件名,一个用于文本文件内容,因此我可以在脚本的其他部分使用它们来根据需要输出。

我理解我需要一个动态数组,因为它需要扩展它所需的循环,它只是我不确定的实现,特别是因为它可能是一个二维数组到保持文件名和内容的一致性。这是代码。

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "Notes\"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files
   For Each objFile in colFiles
   If UCase(objFSO.GetExtensionName(objFile.name)) = "TXT" Then

        Files = objStartFolder & objFile.name
        Set objReadFile = objFSO.OpenTextFile(Files, 1)

        strExt = Left(objFile.name, Len(objFile.name)-4)
        strNote = Replace(objReadFile.ReadAll, vbCRLF, "<br>")

    objReadFile.Close

        document.write strExt & "<br><br>"
        document.write strNote & "<br><br>"

    else
    document.write ="File was empty"

    End If
Next

1 个答案:

答案 0 :(得分:3)

您应该能够相当容易地使用二维数组执行此操作。

因为在循环浏览文件时不需要使用Preserve来动态地调整数组大小之前知道文件的数量,而只是声明一个动态数组,然后使用ReDim来设置初始尺寸。

Dim data()

...

Dim index
ReDim data(1, colFiles.Count - 1)

For Each objFile in colFiles
  ...
  data(0, index) = objFile.Name
  data(1, index) = strNote
  index = index + 1
Next
Erase data

...表示现有代码被忽略以强调添加

更新

OP想要一个完整的例子,因为它不清楚上面的代码如何适合他们的例子,所以这里就是这样;

Option Explicit

Dim objFSO, objFolder, colFiles, objFile, objReadFile
Dim objStartFolder, Files, strExt, strNote

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "Notes\"

Set objFolder = objFSO.GetFolder(objStartFolder)

Dim data(), index

Set colFiles = objFolder.Files
'No need for preserve as we have the Count from the colFiles File Collection.
ReDim data(1, colFiles.Count - 1)

For Each objFile in colFiles
  If UCase(objFSO.GetExtensionName(objFile.name)) = "TXT" Then
    Files = objStartFolder & objFile.name
    Set objReadFile = objFSO.OpenTextFile(Files, 1)

    strExt = Left(objFile.name, Len(objFile.name)-4)
    strNote = Replace(objReadFile.ReadAll, vbCRLF, "<br>")

    objReadFile.Close
    'Release resources
    Set objReadFile = Nothing

    document.write strExt & "<br><br>"
    document.write strNote & "<br><br>"

    'Populate the array
    data(0, index) = objFile.Name
    data(1, index) = strNote
    index = index + 1    
  Else
    document.write ="File was empty"
  End If
Next
'Release resources
Erase data
Set colFiles = Nothing
Set objFolder = Nothing
Set objFSO = Nothing

此代码未经测试

我自己测试了一个可以使用cscript

调用的示例
Option Explicit
Dim fs, folder, files, file
Dim i, r, c

Set fs = CreateObject("Scripting.FileSystemObject")

Set folder = fs.GetFolder("C:\")
Set files = folder.Files

Dim data()

ReDim data(1, files.Count -1)

For Each file In files
  data(0, i) = file.Name
  data(1, i) = file.Path
  i = i + 1
Next

For r = LBound(data, 2) To UBound(data, 2)
  For c = LBound(data, 1) To UBound(data, 1)
    WScript.Echo "data(" & c & ", " & r & ") = " & data(c, r)
  Next
Next
Erase data
Set files = Nothing
Set folder = Nothing
Set fs = Nothing

使用cscript.exe从命令行运行此操作,因为wscript.exe会产生大量弹出框。