2

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 
3
  • What's the format of your hosts file? What error message, if any, do you receive? "Not working" is no use as a problem description. Oh, and welcome to Stack Overflow! Commented Mar 28, 2016 at 20:44
  • Edit your question and post your .csv data Commented Mar 28, 2016 at 21:39
  • You can also get inspired by this : Function ReadFile(path,mode) Commented Mar 28, 2016 at 21:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

They're already using Split() problem is they are not doing correctly. Also using Readline() would be fine if they wanted to make sure that the commas were only read line by line into an array. Problem with ReadAll() method is it will also read in the Carriage Return + Linefeed at the end of each line in the CSV not to mention it's less efficient for larger files.
But it makes an array. Single field CSV don't have commas.
Fair point didn't notice the single field bit in the question, for a single line it should work fine. Also think you may have meant single field CSV don't have new lines not commas (ofc they have commas it's a CSV).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.