关于动态数组的困惑 [英] Confusion regarding Dynamic arrays

查看:90
本文介绍了关于动态数组的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



我知道我需要一个动态数组,因此需要在循环中扩展它intsize,这只是我不确定的实现,特别是因为它可能是一个二维数组,用于将文件名及其内容保持在一起。这是代码。

 设置objFSO = CreateObject( Scripting.FileSystemObject)
objStartFolder = Notes\

设置objFolder = objFSO.GetFolder(objStartFolder)

设置colFiles = objFolder.Files
对于colFiles中的每个objFile
如果是UCase(objFSO。 GetExtensionName(objFile.name))= TXT,然后

文件= objStartFolder& objFile.name
设置objReadFile = objFSO.OpenTextFile(Files,1)

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

objReadFile.Close

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

else
document.write =文件为空

如果
下一个
结束pre>

解决方案

您应该能够非常容易地使用二维数组来做到这一点。


因为您知道文件的数量,所以在遍历文件时无需使用保留来动态保持数组大小的变化,而只需声明一个动态数组并然后使用 ReDim 设置初始尺寸。

  Dim data()

...

暗淡索引
ReDim data(1,colFiles.Count-1)

每个objFile在colFiles中
...
data(0,index)= objFile.Name
data(1,index)= strNote
index = index + 1
下一个
擦除数据

... 表示省略了现有代码,以强调增加的地方


更新:


OP想要一个完整的示例b因为尚不清楚上面的代码如何适合他们的示例,所以在这里;

  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(),索引

Set colFiles = objFolder.Files
'无需保留,因为我们拥有colFiles File Collection中的Count。
ReDim data(1,colFiles.Count-1)

对于colFiles中的每个objFile
如果UCase(objFSO.GetExtensionName(objFile.name))= TXT;然后
Files = objStartFolder& objFile.name
设置objReadFile = objFSO.OpenTextFile(Files,1)

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

objReadFile.Close
'释放资源
设置objReadFile = Nothing

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

'填充数组
data(0,index)= objFile.Name
data(1,index)= strNote
index = index + 1
否则
document.write =文件为空;
如果If
结束
'释放资源
擦除数据
设置colFiles = Nothing
设置objFolder = Nothing
设置objFSO = Nothing

此代码未经测试


我用一个可以使用cscript调用的示例测试了自己

  Option Explicit 
Dim fs ,文件夹,文件,文件
Dim i,r,c

设置fs = CreateObject( Scripting.FileSystemObject)

设置文件夹= fs.GetFolder ( C:\)
设置文件=文件夹。文件

Dim data()

ReDim data(1,files.Count -1 )

每个文件中的文件
data(0,i)= file.Name
data(1,i)= file.Path
i = i + 1
下一个

对于r = LBound(数据,2)到UBound(数据,2)
对于c = LBound(数据,1)到UBound(数据,1)
WScript.Echo data(& c&,& r&)= & data(c,r)
下一个
下一个
擦除数据
设置文件=什么都没有
设置文件夹=什么都没有
设置fs =什么都没有

使用 cscript.exe 作为 wscript.exe 将产生大量弹出框。


Ok so I am using the code below to read and display text files into a HTA with VBScript, it loops through all the text files present in a folder (Notes). I have it parsing returns and have removed the file extension from display.

What I would like to do is build 2 arrays, one for the file names, one for the text file content so I can use them in other parts of the script to output as needed.

I understand I need a dynamic array as such within the loop it needs to expand it's intsize, it's just the implementation I am unsure of, particulary as it could probably be a 2 dimensional array to keep the filename and it's content together. Here is the code.

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

解决方案

You should be able to do this using a 2 dimensional array fairly easily.

Because you know the number of files before there is no need to use Preserve to dynamically keep resizing the array as you loop through the files, instead just declare a dynamic array and then use ReDim to set the initial dimensions.

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

... denotes existing code omitted to emphasizes the additions

Update:

OP would like a full example because it's not clear how the above code fit's into their example, so here goes;

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

This code is untested

I tested this myself with an example that you can call using 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

Run this from the command line using cscript.exe as wscript.exe will produce a ton of popup boxes.

这篇关于关于动态数组的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆