Read File contents into a VB array

VB Script - Read and Write Text file using an Array

The attached project will check for the existence of a folder/file and create it if not present. The file will contain four lines of data. A numeric value that is prompted for when the file is created and then 3 subsequent number values. for example:
Quote
1
2
3
4

If the file exists or after it is created, the program will read the file content into an Array(), the Array() is then written into a new text file. This example could be used to hold variable(s) outside of the CMM-Manager application. i.e., Pass a variable from one program run to another.

Maybe you need to run four programs in sequence (myproj1.cmp, myproj2.cmp, myproj2.cmp & myproj4.cmp). You need to prompt for Serial numbers, but you do not want to have an operator waiting at the machine to enter these values at the beginning of each program run. You could write the values to a text file from a prompt / inputbox() and then launch the subsequent program(s) using External Command and read the correct line of the text file to read the serial number for subsequent runs.


Info
See last section for simplified example.

Scripts

Variable to control filename:

CODE: 

strPartNum = "test part"


Create Files and Folders:

CODE: 

'Dimension variables and Arrays
Dim objFSO
Dim SerialArr(3)
Dim strSerialTxt, i
StopScript = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
strPubFolder = "C:\Users\Public\CMM-Manager"        
strSerialFile = strPubFolder + "\part_num_" + strPartNum + ".txt"

'Check for folder and create if doesn't exist
If Not objFSO.FolderExists(strPubFolder) Then 
  objFSO.CreateFolder(strPubFolder)
End If

'Check if file exists
'If does not exist create it and populate 4 lines with serial numbers
If Not objFSO.FileExists(strSerialFile) Then
  Do Until strFirstSerial >0
    strFirstSerial = InputBox("This is the first " + strPartNum + " run on this machine.  What is the starting serial number?")
    If strFirstSerial = "" Then
      endProg = MsgBox("Are sure you want to cancel?", 4)
      If endProg = 6 Then
        StopScript = 1
        Exit Do       
      End If
    End If     
    If Not strFirstSerial > 0 Then
      MsgBox("Please enter a valid serial number")
    End If
  Loop
  If StopScript = 0 Then
    'Write text file
    Set strSerialTxt = objFSO.CreateTextFile(strSerialFile)
    strSerialTxt.WriteLine(strFirstSerial)
    nxtSerial = strFirstSerial + 1
    strSerialTxt.WriteLine(nxtSerial)
    nxtSerial = nxtSerial + 1
    strSerialTxt.WriteLine(nxtSerial)
    nxtSerial = nxtSerial + 1
    strSerialTxt.WriteLine(nxtSerial)
    strSerialTxt.Close
  End if
End If


Read File contents into an array:

CODE:

'Open file
If StopScript = 0 Then
  Set objFile = objFSO.OpenTextFile(strSerialFile, 1)
  i = 0
  
  'readfile
  Do While objFile.AtEndOfStream = False
    SerialArr(i) = objFile.ReadLine
    i = i + 1
  Loop
  objFile.Close
  
  strSerial1 = SerialArr(0)
  strSerial2 = SerialArr(1)
  strSerial3 = SerialArr(2)
  strSerial4 = SerialArr(3)
  
  MsgBox(strSerial1)
  MsgBox(strSerial2)
  MsgBox(strSerial3)
  MsgBox(strSerial4)
End If

Write an array into a new text file:

CODE: 

'Write out Serial numbers
If StopScript = 0 Then
  'Open File
  strSerialFile = strPubFolder + "\part_num_" + strPartNum + "_OUT.txt"  
  Set strSerialTxt = objFSO.CreateTextFile(strSerialFile)
  
  'Write to file
  strSerialTxt.WriteLine(strSerial1 + "_OUT")
  strSerialTxt.WriteLine(strSerial2 + "_OUT")
  strSerialTxt.WriteLine(strSerial3 + "_OUT")
  strSerialTxt.WriteLine(strSerial4 + "_OUT")
  'Close file
  strSerialTxt.Close
End If 

Simplified Version

Write VB variable(s) into a new text File:

  1. 'create file to store XYZ data
  2. Dim FSO
  3. Set FSO = CreateObject("Scripting.FileSystemObject")
  4. Set OutPutFile = FSO.OpenTextFile("C:\Users\Public\XYZ.dat" ,2 , True)
  5. x = InputBox("Enter X value", "Enter Value", "1.5")
  6. y = InputBox("Enter Y value", "Enter Value", "2.5")
  7. OutPutFile.WriteLine(CStr(x) + "," + CStr(y) + ",0")
  8. 'close text file
  9. OutPutFile.Close
  10. Set FSO= Nothing
  11. MsgBox("Open ""C:\Users\Public\XYZ.dat"" to view file contents")