+ Post New Thread
Results 1 to 12 of 12
Scripts Thread, vbscript to read a txt file and use the info in it as variables in the script in Coding and Web Development; Hi Need some help? Need a vbscript to read a txt file and use the info in it as variables ...
  1. #1

    Join Date
    Feb 2011
    Posts
    37
    Thank Post
    2
    Thanked 1 Time in 1 Post
    Rep Power
    0

    vbscript to read a txt file and use the info in it as variables in the script

    Hi

    Need some help?

    Need a vbscript to read a txt file and use the info in it as variables in the script. Example of txt file is below

    Package_location=\\server\packages
    SYSusername=sysbob
    SYSpassword=654321
    DBlocation=DBserver
    DBUsername=dbbob
    SYSpassword=123456

    I have got this script but not sure how to get it to do what I want to

    Const ForReading = 1

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile _
    ("c:\scripts\name and value.txt", ForReading)

    Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , "=")
    Wscript.Echo "name: " & arrServiceList(0)
    For i = 1 to Ubound(arrServiceList)
    Wscript.Echo "value: " & arrServiceList(i)
    Next
    Loop

    What this does is loop through the txt file and displays it in an echo box

    So reading the first line it would show name: Package_location and then value:\\server\packages what I need it to do is take the value from letís say Package_location and set that a variable,

    Hope this is clear

    thank you for any help you can provide

  2. #2

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,654
    Thank Post
    324
    Thanked 506 Times in 474 Posts
    Rep Power
    177
    Honestly not sure why you wouldn't just put it inside script (unless there.'s lots?)

    But it depends if it's always same amount of lines?

    If yes you could remove the loop and just cycle through file linking to cars, but that wouldn't work if the files change.

    If files change you could dynamically create cars on-the-fly but it'd either be random name, or as "left of ="

    If youcould answer that will try to knock up script later, or tomorrow if you want

    Steve

  3. #3

    Join Date
    Feb 2011
    Posts
    37
    Thank Post
    2
    Thanked 1 Time in 1 Post
    Rep Power
    0
    there are a lots of scripts so any change would mean lots of changes to the scripts the lines in the txt files would change / get added to over time and development so i would need the script to read / test the value to the right of the = and use the value to the left of the = as the variable each time to get the new value or current values.

    thank you

  4. #4

    Join Date
    Feb 2011
    Posts
    37
    Thank Post
    2
    Thanked 1 Time in 1 Post
    Rep Power
    0
    thinking about it the values to the right of the = will not change but the values to the leeft of the = will not sure if i have made that clear very late and very tired

  5. #5
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,936
    Thank Post
    114
    Thanked 272 Times in 250 Posts
    Rep Power
    104
    Assuming the order of the config is the same in every file I would read the each line into an array and reference it that way


    Package_location=\\server\packages
    SYSusername=sysbob
    SYSpassword=654321
    DBlocation=DBserver
    DBUsername=dbbob
    SYSpassword=123456


    Code:
    Dim myArray(6)
    Const ForReading = 1
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile _
    ("c:\scripts\name and value.txt", ForReading)
    j = 0
    Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    j = j + 1
    arrServiceList = Split(strNextLine , "=")
    Wscript.Echo "name: " & arrServiceList(0)
    For i = 1 to Ubound(arrServiceList)
    Wscript.Echo "value: " & arrServiceList(i)
    myarray(j) =  arrServiceList(1)
    Next
    Loop
    For i = 1 to Ubound(myarray)
    do something with myarray(i)
    next

  6. #6

    Join Date
    Feb 2011
    Posts
    37
    Thank Post
    2
    Thanked 1 Time in 1 Post
    Rep Power
    0
    Unfortunately ChrisH the txt file may change in the future and if it does then I would be back to square one of changing all the scripts, but thank you

  7. #7
    dwhyte85's Avatar
    Join Date
    Mar 2009
    Location
    Berkshire
    Posts
    1,199
    Thank Post
    149
    Thanked 143 Times in 129 Posts
    Rep Power
    75
    Quote Originally Posted by NETKILLER View Post
    Unfortunately ChrisH the txt file may change in the future and if it does then I would be back to square one of changing all the scripts, but thank you
    With ChrisH's order doesn't matter... as long as they have x=y in format. You could create a multidimensional array to hold x and y, enum and create an array of your defined bits... so could then say after declaration whatever.leftpart and whatever.rightpart and have both bits of information.

  8. #8

    Join Date
    Feb 2011
    Posts
    37
    Thank Post
    2
    Thanked 1 Time in 1 Post
    Rep Power
    0
    Hey all

    I have been able to write a script that will read the file and write variables; however I have come to the sad conclusion that I would still have to make changes to all the scripts if I was to add to the txt file, is there a “include” command / function in vbscript that works like in PHP? I will be doing a bit of searching to try and find out but if you can help

    Than kyou

  9. #9

    Join Date
    Feb 2011
    Posts
    37
    Thank Post
    2
    Thanked 1 Time in 1 Post
    Rep Power
    0
    hey thank you all for your help i have come across this ExecuteGlobal, this looks to do what i want so will work on this

  10. #10

    Join Date
    Jan 2014
    Posts
    2
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Hi Chris,
    not sure if you will get this but taking your below script how would you use it or recode it for a
    defined array in the vbscript and not from a text file ?
    Regards
    Greg


    Dim myArray(6)
    Const ForReading = 1
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile("c:\scripts\name and value.txt", ForReading)
    j = 0
    Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    j = j + 1
    arrServiceList = Split(strNextLine , "=")
    Wscript.Echo "name: " & arrServiceList(0)
    For i = 1 to Ubound(arrServiceList)
    Wscript.Echo "value: " & arrServiceList(i)
    myarray(j) = arrServiceList(1)
    Next
    Loop For i = 1 to Ubound(myarray)
    do something with myarray(i) next
    Last edited by robochop; 22nd January 2014 at 09:17 AM.

  11. #11
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,936
    Thank Post
    114
    Thanked 272 Times in 250 Posts
    Rep Power
    104
    To define a static array just use:

    Code:
    Dim myArray(6)
    myArray(0) = "Value1"
    myArray(1) = "Value2"
    myArray(2) = "Value3"
    myArray(3) = "Value4"
    myArray(4) = "Value5"

  12. #12

    Join Date
    Jan 2014
    Posts
    2
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Thanks Chris.
    It's your actual do and for loops in your script below that i am interested in.

    So if my array -

    Dim myArray(6)
    myArray(0) = "Value1=emailaddress1"
    myArray(1) = "Value2=emailaddress2"
    myArray(2) = "Value3=emailaddress3"
    myArray(3) = "Value4=emailaddress4"
    myArray(4) = "Value5=emailaddress5"

    How can i use your below script to read each value and then split by finding the "=" sign and then get the emailaddress value
    Regards

    j = 0
    Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    j = j + 1
    arrServiceList = Split(strNextLine , "=")
    Wscript.Echo "name: " & arrServiceList(0)
    For i = 1 to Ubound(arrServiceList)
    Wscript.Echo "value: " & arrServiceList(i)
    myarray(j) = arrServiceList(1)
    Next
    Loop For i = 1 to Ubound(myarray)
    do something with myarray(i) next

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 38
    Last Post: 1st August 2011, 11:18 PM
  2. Replies: 0
    Last Post: 21st February 2011, 08:56 AM
  3. WSUS How to install and use????
    By ipconfig/HELP in forum Windows
    Replies: 33
    Last Post: 1st February 2011, 01:27 PM
  4. Replies: 12
    Last Post: 29th September 2008, 10:57 AM
  5. Strong passwords: How to create and use them
    By FN-GM in forum General Chat
    Replies: 0
    Last Post: 15th August 2007, 11:17 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •