Aaron O
Aaron O

Reputation: 121

vbscript for each array

I'm looking to pull a text document or single field CSV document into an array then loop through the array.

I've been through a few examples on here but continue to fail to get the script working.

Any insight would be appreciated and sorry if I missed a previous useful posting.

    #$language = "VBScript"
#$interface = "1.0"

crt.Screen.Synchronous = True

Sub Main
Set fso = CreateObject("Scripting.FileSystemObject")

Set hosts = fso.OpenTextFile("C:\Users\XXXX\Documents\2016\3 SmartServices\SNMPHosts.txt")



Do Until hosts.AtEndOfStream
    strNextLine = hosts.Readline 
    arrServiceList = Split(strNextLine , ",")   
Loop 

hosts.Close

For Each item In hosts
    crt.Screen.Send "telnet " & item & chr(13)
    crt.Screen.WaitForString "username: "
    crt.Screen.Send "UUUU" & chr(13)
    crt.Screen.WaitForString "password: "
    crt.Screen.Send "PPPP" & chr(13)
    crt.Screen.WaitForString ">"
    crt.Screen.Send "en" & chr(13)
    crt.Screen.Send "PPPP" & chr(13)
    crt.Screen.WaitForString "#"
    crt.Screen.Send "conf t" & chr(13)
    crt.Screen.WaitForString "(config)#"
    crt.Screen.Send "COMMAND STATIC TEXT" & chr(13)
    crt.Screen.Send "COMMAND STATIC TEXT" & chr(13)
    crt.Screen.WaitForString "(config)#"
    crt.Screen.Send "end" & chr(13)
    crt.Screen.WaitForString "#"
    crt.Screen.Send "wr" & chr(13)
    crt.Screen.WaitForString "#"
    crt.Screen.Send "exit" & chr(13)
    crt.Screen.WaitForString "WAIT END STRING"
Next
End Sub 

Upvotes: 2

Views: 2596

Answers (1)

user6017774
user6017774

Reputation:

The split command is what you need. So ReadAll rather than ReadLine

MyVar = Split(hosts.ReadAll, vbcrlf)

For Each thing in MyVar
    Msgbox Replace(thing, ",", "")
Next

From Help at https://www.microsoft.com/en-au/download/details.aspx?id=2764

Split Function

Returns a zero-based, one-dimensional array containing a specified number of substrings.

Split(expression[, delimiter[, count[, compare]]])

Use replace to remove the comma.

Upvotes: 1

Related Questions