Reputation: 139
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
Upvotes: 3
Views: 136
Reputation: 16672
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
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.
Upvotes: 3