3

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
Ctrlaltdenied
  • 139
  • 3
  • 11

1 Answers1

3

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.

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
  • Sorry Lankymart, I am ok with logic statements, settings variables and basic code, but arrays confuse me a little. I shall need a little more explaination than that so I can comprehend. Could you please edit your post and include your code with mine, and explain methods of outputting the data (say with innerhtml, even just a variable). PS: The size of the array would only be limited by the amount of txt files in the Notes directory. – Ctrlaltdenied Oct 14 '15 at 16:41
  • @Ctrlaltdenied The code should just fit around your existing code but will modify the answer later. Out at the moment and commenting via my mobile. – user692942 Oct 14 '15 at 16:58
  • Fair enough fella, I think I have an idea where it would go, but I don't quite *get* arrays yet, and how they can be manipulated. I'd rather understand what it is I'm putting in. Thanks, and no issues. – Ctrlaltdenied Oct 14 '15 at 16:59
  • 1
    @Ctrlaltdenied When it comes to 2 dimensional arrays easiest way to think about them is like columns and rows in a table. The 1st dimension is the columns and the 2nd dimension is the rows, so in this example you have 2 columns and however many rows dictated by `colFiles.Count - 1`. – user692942 Oct 14 '15 at 17:25