# Solved: VBS To Read Text File Into Array



## jo15765 (Oct 11, 2011)

I have a text file that is full of words, literally it is, it is going to be used for a dictionary. I need a vb script that will open the tet file, read all elements into an array, then allow me to randomly select an element from that array. Well, I have it all down except how to open the text file and read the contents into an array. Can someone assist with that? What I plan to do after I have my array is the below...randomly rename the entire data center.

```
Dim computerName
computerName=Array("first","car","cat","dog")
Randomize
MsgBox computerName(Int(Rnd*4))
```


----------



## Guest (Nov 10, 2014)

VBScript:


```
Const ForReading = 1    
Set objFSO = CreateObject("Scripting.FileSystemObject")  
Set objTextFile = objFSO.OpenTextFile ("c:\words.txt", ForReading) 
Dim wordArray(1)
wordCount = 0 
Do Until objTextFile.AtEndOfStream      
    strNextLine = objTextFile.Readline 
    wordCount = wordCount + 1
    ReDim Preserve wordArray(wordCount)
    wordArray(wordCount-1) = strNextLine
 Loop
```


----------



## jo15765 (Oct 11, 2011)

Thank you for that!!

I tried to combine your cdoe with me, and

My code is erroring. Probably because I am not doing the random naming properly

```
Dim computername
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\dictionary.txt", ForReading)
Dim wordArray(1)
wordCount = 0

Do Until objTextFile.AtEndOfStream
	strNextLine = objTextFile.ReadLine
	wordCount = wordCount + 1
	Redim Preserve wordArray(wordCount)
	wordArray(wordCount-1) = strNextLine
Loop

objTextFile.Close

For Each computername In wordArray
	Randomize
	strComputer = computername(Int(Rnd*4344))
Next

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")

For Each objComputer in colComputers
    err = objComputer.Rename(computername)
Next
```


----------



## Squashman (Apr 4, 2003)

jo15765 said:


> What I plan to do after I have my array is the below...randomly rename the entire data center.


Ummmmm. Why!


----------



## Guest (Nov 11, 2014)

Note asWords stands for "array of string words". I get cannot create activex object on the line

oWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & sComputername & "\root\cimv2")

I'm not sure what your trying to do.


```
'http://www.vb-helper.com/howto_net_randomize_array.html
    Private Sub RandomizeArray(ByVal items() As String)
        Dim max_index As Integer = items.Length - 1
        Dim rnd As New Random
        For i As Integer = 0 To max_index - 1
            ' Pick an item for position i.
            Dim j As Integer = rnd.Next(i, max_index + 1)
            ' Swap them.
            Dim temp As String = items(i)
            items(i) = items(j)
            items(j) = temp
        Next i
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sComputername As String
        Const ForReading As Integer = 1
        Dim oFSO, oTextFile As Object
        oFSO = CreateObject("Scripting.FileSystemObject")
        oTextFile = oFSO.OpenTextFile("dictionary.txt", ForReading)
        Dim asWords(1), sNextLine As String
        Dim iWordCount As Integer = 0

        Do Until oTextFile.AtEndOfStream
            sNextLine = oTextFile.ReadLine
            iWordCount = iWordCount + 1
            ReDim Preserve asWords(0 To iWordCount - 1)
            asWords(iWordCount - 1) = sNextLine
        Loop
        oTextFile.Close()
        RandomizeArray(asWords)

        For Each sComputername In asWords
            Dim oWMIService, oComputers, oErr As Object
            oWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" & sComputername & "\root\cimv2")

            oComputers = oWMIService.ExecQuery("Select * from Win32_ComputerSystem")

            For Each oComputer In oComputers
                oErr = oComputer.Rename(sComputername)
            Next
        Next sComputername
        
    End Sub
```


----------

